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

时间:2021-11-11 07:42:00

I follow link https://docs.microsoft.com/en-us/aspnet/core/data/entity-framework-6 to do this. But when I run prọect, It occur error

我遵循链接https://docs.microsoft.com/en- us/aspnet/core/data/entityframework6来实现这一点。但当我运行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()

配置系统未能初始化system .Configuration. clientconfigurationsystem。EnsureInit System.Configuration.ClientConfigurationSystem configKey(字符串)。PrepareClientConfigSystem System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem sectionName(字符串)。GetSection System.Configuration.ConfigurationManager sectionName(字符串)。GetSection(string sectionName) System.Configuration.ConfigurationManager.get_ConnectionStrings() system . data . entity. internes.appconfig .ctor() system . data . entity. internes.appconfig .cctor()

raw exception details

生异常的细节

TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. 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) ....

TypeInitializationException: 'System.Data.Entity.Internal的类型初始化器。AppConfig的抛出一个异常。System.Data.Entity.Internal.LazyInternalConnection . .ctor(context DbContext, string nameOrConnectionString) System.Data.Entity.DbContext。ctor(string nameOrConnectionString)复位器。ef6 . context. mydbcontext .ctor() .ctor()。实现。VisitCreateInstance(CreateInstanceCallSite CreateInstanceCallSite ServiceProvider提供者)....

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 framework 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核心2.0中。ConnectionString获取appset上的ConnectionString值。json和MyDbContext是由DI create 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核心2.0时,这是错误的

ConfigurationErrorsException: Unrecognized configuration section system.data.

ConfigurationErrorsException:无法识别的configuration部分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. I found on nuget but only found this library support with .net standard 1.2. I think can't use entity framework 6 with asp.net core 2.0, wait for next update.

更新,我发现了问题,也许。net core 2.0不支持system.Data.Common。我在nuget上找到了,但只找到了。net标准1.2的库支持。我认为不能使用实体框架6与asp.net核心2.0,等待下次更新。

2 个解决方案

#1


1  

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命令(如启用-迁移)所需的所有功能。

Have you put EF6 in a separate .net 4.6 project? or try using EF Core instead? https://docs.microsoft.com/en-us/ef/core/

你有没有把EF6放到一个单独的。net 4.6项目中?或者试试使用EF核心?https://docs.microsoft.com/en-us/ef/core/

#2


0  

I could not get EF6 to work with ASP.NET Core 2 either. Got the exact same exception as soon as it tried to instantiate my DbContext (which targeted .NET 4.7). I followed https://github.com/tonysneed/Demo.AspNetCore2.EF6 to no success.

我不能让EF6和ASP一起工作。净酷睿2。当它试图实例化我的DbContext(目标是。net 4.7)时,得到了完全相同的异常。我跟随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). 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.

这可能是一个愚蠢的“变通方法”,但是使用ASP。NET Core 1.1, EF6运行良好(步骤为s.https://docs.microsoft.com/en - us/aspnet/core/data/entityframework6? view=aspnetcore-2.0)。我猜想这可能是因为。net标准2.0没有提供一些包,尽管我一直认为它是向后兼容的。

#1


1  

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命令(如启用-迁移)所需的所有功能。

Have you put EF6 in a separate .net 4.6 project? or try using EF Core instead? https://docs.microsoft.com/en-us/ef/core/

你有没有把EF6放到一个单独的。net 4.6项目中?或者试试使用EF核心?https://docs.microsoft.com/en-us/ef/core/

#2


0  

I could not get EF6 to work with ASP.NET Core 2 either. Got the exact same exception as soon as it tried to instantiate my DbContext (which targeted .NET 4.7). I followed https://github.com/tonysneed/Demo.AspNetCore2.EF6 to no success.

我不能让EF6和ASP一起工作。净酷睿2。当它试图实例化我的DbContext(目标是。net 4.7)时,得到了完全相同的异常。我跟随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). 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.

这可能是一个愚蠢的“变通方法”,但是使用ASP。NET Core 1.1, EF6运行良好(步骤为s.https://docs.microsoft.com/en - us/aspnet/core/data/entityframework6? view=aspnetcore-2.0)。我猜想这可能是因为。net标准2.0没有提供一些包,尽管我一直认为它是向后兼容的。