未能设置数据库初始化程序实体框架

本文关键字:程序 实体 框架 初始化 数据库 设置 | 更新日期: 2023-09-27 18:05:32

我正在通过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."

据我所知,它一定在我的网上。

<?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; }   
    }

最后是chorusinitializer。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();
        }
    }
}

未能设置数据库初始化程序实体框架

您给出的代码中Context和初始化器的命名空间都是Chorus.WebAPI.DAL

所以在你的web.config中更新这个:

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

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