gpt4 book ai didi

c# - 无法设置数据库初始值设定项 Entity Framework

转载 作者:太空宇宙 更新时间:2023-11-03 15:36:32 24 4
gpt4 key购买 nike

我正在使用 Entity FrameworkWeb API,我遇到了下面的错误

"Failed to set database initializer of type 'Chorus.DAL.ChorusInitializer, Chorus' 
for DbContext type 'Chorus.DAL.ChorusContext, Chorus' specified in the application
configuration. See inner exception for details."

(添加)这是 InnerException

"Could not load file or assembly 'Chorus' or one of its dependencies. The system cannot find the file specified."

据我所知,它必须在我下面的 web.config 中。

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=301879
-->
<configuration>
<configSections>

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Chorus.WebAPI-20150610073430.mdf;Initial Catalog=aspnet-Chorus.WebAPI-20150610073430;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="ChorusContext" connectionString="Data Source=(localdb)\MSSQLLocalDB; Initial Catalog=ChorusContext-20150720204839; Integrated Security=True; MultipleActiveResultSets=True; AttachDbFilename=|DataDirectory|ChorusContext-20150720204839.mdf" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings></appSettings>
<system.web>
<authentication mode="None" />
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</modules>
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.Edm" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Data.OData" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Spatial" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.6.4.0" newVersion="5.6.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<contexts>
<context type="Chorus.DAL.ChorusContext, Chorus">
<databaseInitializer type="Chorus.DAL.ChorusInitializer, Chorus" />
</context>
</contexts>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

我查看了我的 Context 和 Initializer,两者都是公开的并且可用。

这是创建新上下文对象的 Get 请求

public class OrdersController : ApiController
{

private ChorusContext db = new ChorusContext();
// GET: api/Orders
[EnableQuery()]
public IHttpActionResult Get()
{

using (ChorusContext db = new ChorusContext())
{
var result = db.Orders.ToList();
{
result = result.OrderBy(s => s.OrderId).ToList();
}

return this.Ok(result);
}


}

这是 ChorusContext.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity;

namespace Chorus.WebAPI.DAL
{
public class ChorusContext : DbContext
{
public ChorusContext() : base()
{
this.Database.Log = s => System.Diagnostics.Debug.WriteLine(s);
}

public DbSet<Chorus.WebAPI.Models.Order> Orders { get; set; }
public DbSet<Chorus.WebAPI.Models.Customer> Customers { get; set; }
public DbSet<Chorus.WebAPI.Models.Warranty> Warrantys { get; set; }
}

最后是 ChorusIntializer.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Chorus.WebAPI.DAL
{
public class ChorusIntializer : System.Data.Entity.DropCreateDatabaseAlways<ChorusContext>
{
protected override void Seed(ChorusContext context)
{
var orders = new List<Chorus.WebAPI.Models.Order>
{
new Models.Order {data went here},

};

orders.ForEach(s => context.Orders.Add(s));
context.SaveChanges();
}
}
}

最佳答案

您提供的代码中上下文和初始化程序的命名空间是 Chorus.WebAPI.DAL

因此在您的 web.config 中进行更新:

<context type="Chorus.WebAPI.DAL.ChorusContext, Chorus">
<databaseInitializer type="Chorus.WebAPI.DAL.ChorusInitializer, Chorus" />
</context>

您还需要确保此配置部分中程序集的名称是正确的 - 我们无法从给定的信息中看出它是什么,但它应该是 Chorus.dll。如果是其他情况,您也需要进行设置。

关于c# - 无法设置数据库初始值设定项 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31715042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com