类型' System.InvalidOperationException'的例外情况发生在EntityFramework.dll中但未在用户代码中处理,C#

时间:2015-10-13 06:31:29

标签: c# .net database entity-framework

我正在尝试使用数据库和实体框架。

这是我的数据库:

Database

我有以下创建上下文的文件:

namespace SportsStore.domain.Concrete
{
    //Associate the model with the database
    //This class then automatically defines a property for each table in the database that I want to work with. 
    public class EFDbContext : DbContext { 


        public DbSet<Product> Products { get; set; }

        /*protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        }*/
    }
}

这是我初始化上下文类的文件:

namespace SportsStore.domain.Concrete
{
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext context = new EFDbContext();

        public IEnumerable<Product> Products
        {
            get { return context.Products.ToList(); } 
        }
    }
}

当我运行我的应用程序时,出现以下错误:

  

类型&#39; System.InvalidOperationException&#39;的例外情况发生在EntityFramework.dll中但未在用户代码中处理

     

附加信息:无法设置类型&#39; SportsStore.domain.Concrete,EFProductRepository&#39;的数据库初始化程序。对于DbContext类型&#39; SportsStore.domain.Concrete,EFDbContext&#39;在应用程序配置中指定。有关详细信息,请参阅内部异常。

这是内部异常的详细信息:

  

无法加载文件或程序集EFDbContext或其依赖项之一。系统找不到文件EFDbContext

这是我的App.config:

    <?xml version="1.0" encoding="utf-8"?>
    <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>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
      <connectionStrings>
        <add name="EFDbContext" connectionString="data source=(LocalDb)\MSSQLLocalDB;initial catalog=SportsStore.domain.Concrete;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
      </connectionStrings>
    </configuration>

这是我的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=301880
      -->
    <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>
      <appSettings>
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5.2" />
        <httpRuntime targetFramework="4.5.2" />
      </system.web>
      <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
          <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.WebPages" 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="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
      <system.codedom>
        <compilers>
          <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
          <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
        </compilers>
      </system.codedom>
      <entityFramework>

        <contexts>
          <context type="SportsStore.domain.Concrete, EFDbContext">
            <databaseInitializer type="SportsStore.domain.Concrete, EFProductRepository" />
          </context>
        </contexts>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>

任何可以帮助我的人?我是.NET中的Entity框架和数据库的新手 它如何知道使用哪个数据库?

2 个答案:

答案 0 :(得分:1)

Web.config文件中,您有以下配置:

<configuration>
  <entityFramework>
    <contexts>
      <context type="SportsStore.domain.Concrete, EFDbContext">
        <databaseInitializer type="SportsStore.domain.Concrete, EFProductRepository" />
      </context>
    </contexts>
  </entityFramework>
</configuration>

在此处指定程序集EFDbContext包含应由程序集SportsStore.domain.Concrete中的初始值设定项类型SportsStore.domain.Concrete初始化的上下文类型EFProductRepository

其中一些名字看起来相当不正统。结合系统无法找到文件EFDbContext 的基础错误,我猜你需要完成这个配置并修复它。

  • 你有一个或两个组件吗?如果只有一个,则在配置中指定正确的名称。
  • 上下文和初始化程序是否由正确的类型指定?如果不正确的类型名称。
  • 你甚至有一个初始化器吗?如果没有从Web.config完全删除配置。 EFProductRepository肯定不是初始化程序。

您可以在配置文件中指定类型:

[Fully qualified name of type including namespace], [Name of assembly without .DLL]

所以要指定你的上下文,假设程序集名称为EFDbContext,你可以这样写:

SportsStore.domain.Concrete.EFDbContext, EFDbContext

根据您获得的错误,程序集名称可能不正确,但只有您知道正确的名称。

答案 1 :(得分:0)

我总是在PM控制台(包管理器控制台)上使用以下命令在VS ASP.NET脚手架中解决了这个问题

PM> Update-Database  -Force

那应该适合你!