第三篇是Entity Framework升级
修改project.json
把原来 EntityFramework 的包 换成 Microsoft.EntityFrameworkCore
版本从 7.0.0-rc1-final 改为 1.0.0-rc2-final
对照表如下:
RC1 Package | RC2 Equivalent |
EntityFramework.MicrosoftSqlServer 7.0.0-rc1-final | Microsoft.EntityFrameworkCore.SqlServer 1.0.0-rc2-final |
EntityFramework.SQLite 7.0.0-rc1-final | Microsoft.EntityFrameworkCore.SQLite 1.0.0-rc2-final |
EntityFramework7.Npgsql 3.1.0-rc1-3 | NpgSql.EntityFrameworkCore.Postgres <to be advised> |
EntityFramework.SqlServerCompact35 7.0.0-rc1-final | EntityFrameworkCore.SqlServerCompact35 1.0.0-rc2-final |
EntityFramework.SqlServerCompact40 7.0.0-rc1-final | EntityFrameworkCore.SqlServerCompact40 1.0.0-rc2-final |
EntityFramework.InMemory 7.0.0-rc1-final | Microsoft.EntityFrameworkCore.InMemory 1.0.0-rc2-final |
EntityFramework.IBMDataServer 7.0.0-beta1 | Not yet available for RC2 |
EntityFramework.Commands 7.0.0-rc1-final | Microsoft.EntityFrameworkCore.Tools 1.0.0-preview1-final |
EntityFramework.MicrosoftSqlServer.Design 7.0.0-rc1-final | Microsoft.EntityFrameworkCore.SqlServer.Design 1.0.0-rc2-final |
增加EF cli工具
在 project.json 的 tools 配置节中加入
"Microsoft.EntityFrameworkCore.Tools": { "version": "1.0.0-preview1-final", "imports": [ "portable-net45+win8+dnxcore50", "portable-net45+win8" ] }
EF的相关cli命令,由原来的 dnx ef 改为 dotnet ef,具体可以通过 dotnet ef --help 来查看
修改代码中的命名空间
把原来的 Microsoft.Data.Entity 改为 Microsoft.EntityFrameworkCore
这里可以批量查找替换掉
修改Startup.cs
RC2中已经移除了AddEntityFramework()、AddInMemoryDatabase()、AddSqlServer(),所以我们也要在代码中相应的移除掉它们,以我自己的项目中为例子
原来为:
public void ConfigureServices(IServiceCollection services) { #if DEBUG services.AddEntityFramework() .AddInMemoryDatabase() .AddDbContext<EFContext>(option => { option.UseInMemoryDatabase(); }); #else services.AddEntityFramework() .AddSqlServer() .AddDbContext<EFContext>(option => { option.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]); }); #endif services.AddApplicationInsightsTelemetry(Configuration); // Add framework services. services.AddMvc(); }
现在则改为:
public void ConfigureServices(IServiceCollection services) { #if DEBUG services.AddDbContext<EFContext>(option => { option.UseInMemoryDatabase(); }); #else services.AddDbContext<EFContext>(option => { option.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]); }); #endif // Add framework services. services.AddMvc(); }