第三篇是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 配置节中加入
1
2
3
4
5
6
7
|
"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(),所以我们也要在代码中相应的移除掉它们,以我自己的项目中为例子
原来为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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();
}
|
现在则改为:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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();
}
|
相关文章:
原文地址:http://blog.lishewen.com/post/upgrade-thenet-core-rc2-(3)-those-things-the-entity-framework-to-upgrade
.NET社区新闻,深度好文,微信中搜索dotNET跨平台或扫描二维码关注