简体   繁体   English

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

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

Im trying to work with databases and Entity-framework. 我正在尝试使用数据库和实体框架。

Here Is my Database: 这是我的数据库:

数据库

I have the following file that creates an context: 我有以下创建上下文的文件:

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

Here IS my file that Initialize the context-class: 这是初始化上下文类的我的文件:

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

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

When I run my application, I get the following error: 运行应用程序时,出现以下错误:

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型'System.InvalidOperationException'的异常,但未在用户代码中处理

Additional information: Failed to set database initializer of type 'SportsStore.domain.Concrete, EFProductRepository' for DbContext type 'SportsStore.domain.Concrete, EFDbContext' specified in the application configuration. 附加信息:无法为应用程序配置中指定的DbContext类型“ SportsStore.domain.Concrete,EFDbContext”设置数据库类型为“ SportsStore.domain.Concrete,EFProductRepository”的数据库初始化程序。 See inner exception for details. 有关详细信息,请参见内部异常。

Here Is the details message of the inner exception: 这是内部异常的详细信息:

Can not load file or assembly EFDbContext or one of its dependencies. 无法加载文件或程序集EFDbContext或其依赖项之一。 The system can not find the file EFDbContext 系统找不到文件EFDbContext

Here Is my App.config: 这是我的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>

Here is my Web.Config: 这是我的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>

How does it know which database to use? 它如何知道要使用哪个数据库?

I'm new to Entity framework and databases in .NET. 我是.NET中的实体框架和数据库的新手。

In your Web.config file you have the following configuration: 在您的Web.config文件中,您具有以下配置:

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

Here you specify that the assembly EFDbContext contains a context type SportsStore.domain.Concrete that should be initialized by initializer type SportsStore.domain.Concrete in assembly EFProductRepository . 在这里,您指定装配EFDbContext包含上下文类型SportsStore.domain.Concrete应该由初始化类型初始化SportsStore.domain.Concrete组装EFProductRepository

Some of these names looks rather unorthodox. 其中一些名称看起来很不合常规。 Combined with the underlying error that The system can not find the file EFDbContext my guess is that you need to go through this configuration and fix it. 结合系统无法找到文件EFDbContext的潜在错误,我猜是您需要通过此配置并对其进行修复。

  • Do you have one or two assemblies? 您有一个或两个程序集吗? If only one then specify the correct name in the configuration. 如果只有一个,则在配置中指定正确的名称。
  • Are the context and the initializer specified by the correct type? 上下文和初始化程序是否由正确的类型指定? If not correct the type names. 如果不正确,则键入名称。
  • Do you even have an initializer? 你甚至有一个初始化器? If not remove the configuration completely from Web.config . 如果没有,请从Web.config完全删除配置。 EFProductRepository is certainly not an initializer. EFProductRepository当然不是初始化程序。

You specify a type in a configuration file like this: 您可以在配置文件中指定类型,如下所示:

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

So to specify you context assuming the assembly name is EFDbContext you write this: 因此,假设程序集名称为EFDbContext来指定上下文,请编写以下代码:

SportsStore.domain.Concrete.EFDbContext, EFDbContext

The assembly name is probably wrong based on the error that you get but only you know the correct name. 根据您得到的错误,程序集名称可能是错误的,但只有您知道正确的名称。

I have always solved this issues in ASP.NET Scaffolding with VS using the following commando on the PM console (Package Manager Console) 我一直在PM控制台(软件包管理器控制台)上使用以下突击步枪在带有VS的ASP.NET脚手架中解决了此问题

PM> Update-Database  -Force

That should be work for you! 那应该为您工作!

暂无
暂无

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

相关问题 EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中出现“System.InvalidOperationException”类型的异常,但在注册时未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll but was not handled in user code while Registering System.InvalidOperationException &gt;&gt;发生在EntityFramework.dll中,但未在用户代码中处理? - System.InvalidOperationException >> occurred in EntityFramework.dll but was not handled in user code? EntityFramework.SqlServer.dll中发生类型为&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code EntityFramework.dll中出现“System.Xml.XmlException”类型的异常,但未在用户代码中处理 - An exception of type 'System.Xml.XmlException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.Data.SqlClient.SqlException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.SqlClient.SqlException' occurred in EntityFramework.dll but was not handled in user code EntityFramework.dll中发生类型&#39;System.NullReferenceException&#39;的异常,但未在用户代码中处理:执行存储过程 - An exception of type 'System.NullReferenceException' occurred in EntityFramework.dll but was not handled in user code : Executing a stored procedure EntityFramework.dll中发生类型为&#39;System.Data.Entity.Core.MappingException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.Data.Entity.Core.MappingException' occurred in EntityFramework.dll but was not handled in user code System.Core.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - Exception of type 'System.InvalidOperationException' occurred in System.Core.dll but was not handled in user code System.ServiceModel.dll中发生类型&#39;System.InvalidOperationException&#39;的异常,但未在用户代码中处理 - An exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.dll but was not handled in user code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM