Before you start, make sure that you compile against full .NET Framework in your project.json as Entity Framework 6 does not support .NET Core. If you need cross platform features you will need to upgrade to Entity Framework Core.
In your project.json file specify a single target for the full .NET Framework:
"frameworks": {
"net46": {}
}
and then Setup connection strings and dependency injection
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
}
In the Startup class within ConfigureServices add factory method of your context with it’s connection string. Context should be resolved once per scope to ensure performance and ensure reliable operation of Entity Framework.
public void ConfigureServices(IServiceCollection services)
{
services.AddScoped((_) => new ApplicationDbContext(Configuration["Data:DefaultConnection:ConnectionString"]));
// Configure remaining services
}
ntity Framework 6 allows configuration to be specified in xml (in web.config or app.config) or through code. As of ASP.NET Core, all configuration is code-based.
Code-based configuration is achieved by creating a subclass of System.Data.Entity.Config.DbConfiguration and applying System.Data.Entity.DbConfigurationTypeAttribute to your DbContext subclass.
Our config file typically looked like this:
<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>
The defaultConnectionFactory element sets the factory for connections. If this attribute is not set then the default value is SqlConnectionProvider. If, on the other hand, value is provided, the given class will be used to create DbConnection with its CreateConnection method. If the given factory has no default constructor then you must add parameters that are used to construct the object
[DbConfigurationType(typeof(CodeConfig))] // point to the class that inherit from DbConfiguration
public class ApplicationDbContext : DbContext
{
[...]
}
public class CodeConfig : DbConfiguration
{
public CodeConfig()
{
SetProviderServices("System.Data.SqlClient",
System.Data.Entity.SqlServer.SqlProviderServices.Instance);
}
}
This article will show you how to use Entity Framework 6 inside an ASP.NET Core application.
https://docs.asp.net/en/latest/data/entity-framework-6.html