【问题标题】:An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, C#EntityFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码 C# 中处理
【发布时间】:2015-10-13 06:31:28
【问题描述】:

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

这是我的数据库:

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

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(); } 
        }
    }
}

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

EntityFramework.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理

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

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

无法加载文件或程序集 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 中的实体框架和数据库的新手。

【问题讨论】:

  • 解释在你得到的异常中。不幸的是,您没有在实际描述中包含重要部分:有关详细信息,请参阅内部异常。
  • @MartinLiversage:检查我更新的问题。 :)
  • 尝试自己阅读异常信息。您提供的新问题还说有关详细信息,请参阅内部异常。您必须继续阅读,直到找到根本原因,然后希望您能找到问题的答案,或者至少可以提问这里有一个更明智的问题。
  • @MartinLiversage:对不起,我贴错了。再检查一遍。我不知道如何解决它。不知道为什么会出现这个错误。

标签: c# .net database entity-framework


【解决方案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,该上下文类型应由程序集EFProductRepository 中的初始化程序类型SportsStore.domain.Concrete 初始化。

其中一些名称看起来相当不正统。结合系统找不到文件EFDbContext的底层错误,我的猜测是你需要通过这个配置并修复它。

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

您可以像这样在配置文件中指定一个类型:

[包括命名空间的类型的完全限定名称]、[不带 .DLL 的程序集名称]

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

SportsStore.domain.Concrete.EFDbContext, EFDbContext

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

【讨论】:

  • 文件 EFDbContext.cs 和 EFProductRepository.cs 位于名为 Concrete 的文件夹中。该文件夹属于一个名为 SportsStore.domain 的项目。这里的初始化器是什么意思?
  • @Bryan:在您的Web.config 文件中,您已指定您的上下文应使用database intializer,但如果这不是您的意图,则删除&lt;contexts&gt; XML 元素和其中的所有内容。您似乎还将源文件名与程序集名称混合在一起。如果您的程序集名称是 SportsStore.domain,那么这就解释了为什么当您指示 EF 加载 EFDbContext 时会得到 file not found
  • 谢谢。我已经从 XML 中删除了上下文。现在,当我启动我的应用程序时,错误消息消失了,但没有打印任何行。你能解释一下初始化器的用途吗?它们是干什么用的?
  • @Bryan:使用数据库初始化程序来初始化数据库并不奇怪。这通常用于要在修改上下文时修改(迁移)数据库架构的迁移。有关更多信息,请参阅IDatabaseInitializer&lt;TContext&gt;,尤其是实现此接口的文档中提到的类。
  • 谢谢。你有什么建议为什么我启动我的应用程序时没有显示这些行吗?
【解决方案2】:

我一直在 ASP.NET Scaffolding with VS 中使用 PM 控制台(包管理器控制台)上的以下突击队解决了这个问题

PM> Update-Database  -Force

这应该适合你!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-04
    • 2018-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-25
    • 2015-05-30
    相关资源
    最近更新 更多