Reputation: 101
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.
Upvotes: 4
Views: 2758
Reputation: 8844
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.
Upvotes: 1
Reputation: 2388
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/
Upvotes: 1