4

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

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

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) ....

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

    [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

Static class GlobalVariables on Standard class 2.0

This is Error when I run my project asp.net core 2.0 againt

ConfigurationErrorsException: Unrecognized configuration section 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.

3
  • It would be helpful if you included some code, like the constructor for your context and the relevant lines from your Startup class. Commented Dec 4, 2017 at 11:15
  • I update code, pls, help me Commented Dec 4, 2017 at 14:37
  • I had the same issue until I removed the AspNetCore.All metapackage, and added only the packages I require manually. Commented Jun 2, 2018 at 16:12

2 Answers 2

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.

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

Sign up to request clarification or add additional context in comments.

11 Comments

Yes, I put EF6 in a separate .net 4.6.1, and reference to asp.net core 2.0, I did exactly on learn.microsoft.com/en-us/aspnet/core/data/entity-framework-6
Do you have DefaultConnection defined in your appSettings.json ?
"ConnectionStrings": { "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=EF6MVCCore;Trusted_Connection=True;MultipleActiveResultSets=true" }
Yes, but I dont know why.. With asp.net core 1.1, It work perfect, but asp.net core 2.0 don't
This is error on contructor of MyDbContext, I posted my code
|
1

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.

This may be a silly "workaround", but with ASP.NET Core 1.1, EF6 works fine (s. https://learn.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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.