如何在Asp.Net 5中使用Entity Framework 6.x(MVC 6)

我正在使用VS 2015 CTP-6测试新的Asp.Net 5。 由于Entity Framework 7中缺少function,我现在更喜欢使用EF6。

我尝试删除EF7,然后在PM中应用EF6,如下所示:

Uninstall-Package EntityFramework Install-Package EntityFramework -version 6.1.3 

没有返回错误,并且project.json文件似乎相应地更新。 虽然,没有DbContext可用。

这是可能吗? 如果是的话,我该如何从这里开始? 我是否需要web.config以兼容EF6?

是的,这很好用。

您需要在创建上下文时手动设置连接字符串,因为它无法从web.config获取它

所以你可以做到这一点

 public class MyContext : DbContext { public MyContext(string connectionString) : base(connectionString) { } } var context = new MyContext("myConnectionString"); 

如果你想从config.json获取连接字符串,那么试试这个

 IConfiguration configuration = new Configuration().AddJsonFile("config.json"); var connectionString = configuration["Data:DefaultConnection:ConnectionString"]); 

如果你想将上下文注入DI容器,那么我添加了这样的工厂

 public static class MyContextFactory { public static MyContext GetContext() { IConfiguration configuration = new Configuration().AddJsonFile("config.json"); return new MyContext(configuration["Data:DefaultConnection:ConnectionString"]); } } 

然后在startup.cs中添加它

 services.AddTransient((a) => MyContextFactory.GetContext()); 

根据使用的数据库,它可能不像答案那么容易。 如果您使用的是MsSql,则不需要进行任何配置,并且接受的答案非常好。 但是使用LocalDB可能需要一些配置。

例如, MySql需要注册提供者

 [DbConfigurationType(typeof(CodeConfig))] // point to the class that inherit from DbConfiguration public class ApplicationDbContext : DbContext { [...] } public class CodeConfig : DbConfiguration { public CodeConfig() { SetDefaultConnectionFactory(new MySql.Data.Entity.MySqlConnectionFactory()); SetProviderServices("MySql.Data.MySqlClient", new MySql.Data.MySqlClient.MySqlProviderServices()); } } 

PostgreSql需要将提供者注册到entityFramework和system.data部分。 这可以通过使用System.Data.Entity.DbConfiguration.Loaded事件来完成。 Oracle

查看此博客文章,详细解释它: http : //bleedingnedge.com/2015/11/01/entity-framework-6-with-asp-net-5/

你能不能在startup.cs文件中这样做? 节省创建工厂

 // new context on each request services.AddScoped((s) => { return new MyContext(Configuration["Data:MyConnection:ConnectionString"]); }); 

使用RC版本,这将成为:

  var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddEnvironmentVariables(); var Configuration = builder.Build(); var connectionString = Configuration["Data:DefaultConnection:ConnectionString"]; 

在开始之前,请确保在project.json针对完整的.NET Framework进行编译,因为Entity Framework 6不支持.NET Core。 如果您需要跨平台function,则需要升级到Entity Framework Core。

在project.json文件中为完整的.NET Framework指定单个目标:

 "frameworks": { "net46": {} } 

然后设置连接字符串和dependency injection

 public class ApplicationDbContext : DbContext { public ApplicationDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } } 

在ConfigureServices中的Startup类中,使用连接字符串添加上下文的工厂方法。 每个范围应解决一次上下文,以确保性能并确保entity framework的可靠运行。

 public void ConfigureServices(IServiceCollection services) { services.AddScoped((_) => new ApplicationDbContext(Configuration["Data:DefaultConnection:ConnectionString"])); // Configure remaining services } 

ntity Framework 6允许在xml(在web.config或app.config中)或通过代码指定配置。 从ASP.NET Core开始,所有配置都是基于代码的。

通过创建System.Data.Entity.Config.DbConfiguration的子类并将System.Data.Entity.DbConfigurationTypeAttribute应用于DbContext子类来实现基于代码的配置。

我们的配置文件通常如下所示:

           

defaultConnectionFactory元素设置连接的工厂。 如果未设置此属性,则默认值为SqlConnectionProvider 。 另一方面,如果提供了值,则给定的类将用于使用其CreateConnection方法创建DbConnection 。 如果给定工厂没有默认构造函数,则必须添加用于构造对象的参数

 [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); } } 

本文将向您展示如何在ASP.NET Core应用程序中使用Entity Framework 6。 https://docs.asp.net/en/latest/data/entity-framework-6.html