繁体   English   中英

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

[英]An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code, C#

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

这是我的数据库:

数据库

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

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中的实体框架和数据库的新手。

在您的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

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

我一直在PM控制台(软件包管理器控制台)上使用以下突击步枪在带有VS的ASP.NET脚手架中解决了此问题

PM> Update-Database  -Force

那应该为您工作!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM