我正在处理Entity Framework
和Web 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 Request
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();
}
}
}
答案 0 :(得分:0)
您提供的代码中的Context和初始化程序的命名空间是Chorus.WebAPI.DAL
。
所以在你的web.config中进行更新:
<context type="Chorus.WebAPI.DAL.ChorusContext, Chorus">
<databaseInitializer type="Chorus.WebAPI.DAL.ChorusInitializer, Chorus" />
</context>
您还需要确保在此配置部分中程序集的名称是正确的 - 我们无法从给出的信息中看到它是什么,但它应该是Chorus.dll。如果它是别的东西你也需要设置它。