简体   繁体   English

不能将Asp.net核心2.0与实体框架6一起使用

[英]Can't use Asp.net core 2.0 with entity framework 6

I follow link https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6 to do this. 我按照链接https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6执行此操作。 But when I run prọect, It occur error 但是当我运行prọect时,它会发生错误

ConfigurationErrorsException: Configuration system failed to initialize System.Configuration.ClientConfigurationSystem.EnsureInit(string configKey) System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(string sectionName) System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(string sectionName) System.Configuration.ConfigurationManager.GetSection(string sectionName) System.Configuration.ConfigurationManager.get_ConnectionStrings() System.Data.Entity.Internal.AppConfig..ctor() System.Data.Entity.Internal.AppConfig..cctor() ConfigurationErrorsException:配置系统无法初始化System.Configuration.ClientConfigurationSystem.EnsureInit(string configKey)System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(string sectionName)System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(string sectionName)System。 Configuration.ConfigurationManager.GetSection(string sectionName)System.Configuration.ConfigurationManager.get_ConnectionStrings()System.Data.Entity.Internal.AppConfig..ctor()System.Data.Entity.Internal.AppConfig..cctor()

raw exception details 原始异常细节

TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. TypeInitializationException:'System.Data.Entity.Internal.AppConfig'的类型初始值设定项引发了异常。 System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context, string nameOrConnectionString) System.Data.Entity.DbContext..ctor(string nameOrConnectionString) Repositories.EF6.Context.MyDbContext..ctor() Repositories.EF6.Implements.FakeLinkRepository..ctor() System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCreateInstance(CreateInstanceCallSite createInstanceCallSite, ServiceProvider provider) .... System.Data.Entity.Internal.LazyInternalConnection..ctor(DbContext context,string nameOrConnectionString)System.Data.Entity.DbContext..ctor(string nameOrConnectionString)Repositories.EF6.Context.MyDbContext..ctor()Repositories.EF6.Implements .FakeLinkRepository..ctor()System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCreateInstance(CreateInstanceCallSite createInstanceCallSite,ServiceProvider provider)....

I have divided the entity framework into a separate project, Can anyone have idea to fix it 我已将实体框架划分为一个单独的项目,任何人都有想法修复它

This is my code In EF6 project run on .net framework 4.6.1 这是我的代码在.net框架4.6.1上运行的EF6项目

    [DbConfigurationType(typeof(MySqlEFConfiguration))] 
public class MyDbContext : DbContext
        {

            public MyDbContext():base(GlobalVariables.ConnectionString == null ? "name=defaultConnectionString" : GlobalVariables.ConnectionString)
            {

            }
            public MyDbContext(string connectionString):base(connectionString)
            {

            }
           .......

    }

public class MyDbConfiguration : MySqlEFConfiguration
    {
        public MyDbConfiguration()
        {
            SetProviderServices("MySql.Data.MySqlClient", new MySqlProviderServices());
            SetDefaultConnectionFactory(new MySqlConnectionFactory());
        }
    }

My repository 我的存储库

public class MyRepository : Repository
    {
        MyDbContext _context;
        public MyRepository ()
        {
            _context = new MyDbContext();
        }
     .....
}

In asp.net core 2.0 GlobalVariables.ConnectionString get value of connectionString on appSetting.json and MyDbContext is created by DI create MyRepository 在asp.net core 2.0中GlobalVariables.ConnectionString在appSetting.json上获取connectionString的值,而MyDbContext由DI创建MyRepository创建

Static class GlobalVariables on Standard class 2.0 标准类2.0上的静态类GlobalVariables

This is Error when I run my project asp.net core 2.0 againt 当我运行我的项目asp.net core 2.0时,这是错误的

ConfigurationErrorsException: Unrecognized configuration section system.data. ConfigurationErrorsException:无法识别的配置节system.data。

file config is : 文件配置是:

<configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <connectionStrings>
    <add name="defaultConnectionString" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;user id=root;password=admin;database=té;persistsecurityinfo=True" />
  </connectionStrings>
  <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" />
      <provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="MySql.Data.MySqlClient" />
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.10.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>
  </system.data>

Update, I found the problem, maybe .net core 2.0 don't support system.Data.Common. 更新,我发现问题,也许.net core 2.0不支持system.Data.Common。 I found on nuget but only found this library support with .net standard 1.2. 我在nuget上找到了但只发现了.net标准1.2的这个库支持。 I think can't use entity framework 6 with asp.net core 2.0, wait for next update. 我认为不能用asp.net core 2.0的实体框架6,等待下一次更新。

You can't put an EF6 context in an ASP.NET Core project because .NET Core projects don't support all of the functionality that EF6 commands such as Enable-Migrations require. 您不能将EF6上下文放在ASP.NET Core项目中,因为.NET Core项目不支持EF6命令(如Enable-Migrations所需)的所有功能。

Have you put EF6 in a separate .net 4.6 project? 你有没有把EF6放在一个单独的.net 4.6项目中? or try using EF Core instead? 或尝试使用EF Core? https://docs.microsoft.com/en-us/ef/core/ https://docs.microsoft.com/en-us/ef/core/

I could not get EF6 to work with ASP.NET Core 2 either. 我无法让EF6与ASP.NET Core 2一起工作。 Got the exact same exception as soon as it tried to instantiate my DbContext (which targeted .NET 4.7). 一旦尝试实例化我的DbContext(针对.NET 4.7),就会得到完全相同的异常。 I followed https://github.com/tonysneed/Demo.AspNetCore2.EF6 to no success. 我跟着https://github.com/tonysneed/Demo.AspNetCore2.EF6没有成功。

This may be a silly "workaround", but with ASP.NET Core 1.1, EF6 works fine (s. https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view=aspnetcore-2.0 for the steps). 这可能是一个愚蠢的“解决办法”,但随着ASP.NET 1.1的核心,EF6工作正常(S。 https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6?view = aspnetcore-2.0的步骤)。 I guess this may be because of some packages not being available for .NET Standard 2.0, though I always thought it's backward compatible. 我想这可能是因为某些软件包不适用于.NET Standard 2.0,尽管我一直认为它是向后兼容的。

暂无
暂无

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

相关问题 如何使用Asp.Net.Identity和Entity Framework 6在Asp.Net Core 2.0 Target Framework 4.6.1中设置身份? - How can I setup identity in Asp.Net Core 2.0 Target Framework 4.6.1 using Asp.Net.Identity and Entity Framework 6? Asp.net Core 2.0与MYSQL的实体框架数据库优先方法 - Asp.net core 2.0 with entity framework database first approach with MYSQL 无法确定导航属性ASP.NET核心2.0实体框架所代表的关系 - Unable to determine the relationship represented by navigation property ASP.NET core 2.0 Entity Framework ASP.NET Core 2.0 Entity Framework如何访问用户配置文件 - asp.net core 2.0 Entity Framework how to access user profile 没有实体框架但存储过程的 ASP.Net Core 2.0 API OData - ASP.Net Core 2.0 API OData without Entity Framework but stored procedures 我可以使用具有 ASP.NET 成员资格的实体框架吗? - Can I use Entity Framework with ASP.NET Membership? 具有多个关键实体的Asp.net核心Entity Framework 6错误 - Asp.net core Entity Framework 6 error with multiple key entity 更新实体中的ICollection(实体框架6,ASP.NET Core) - Update ICollection in Entity (Entity Framework 6, ASP.NET Core) 在 Entity Framework Core 和 ASP.NET Core 中加载相关数据 - Loading related data in Entity Framework Core and ASP.NET Core 如果支持 .Net Standard 2.0 的 nuget 包,那么我们可以在 asp.net 和 asp.net core 中使用该包吗? - If a nuget Package is .Net Standard 2.0 supported then can we use that package in asp.net as well as asp.net core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM