【发布时间】:2018-05-03 14:32:10
【问题描述】:
我在当前的 .NET Framework 应用程序中进行了大约 40 次迁移。我正在将数据库及其所有存储库移植到 .NET 标准库,因为新应用程序是用 .NET Core 编写的,这会导致存储库的双重实现,并且在 .NET Core 应用程序中,我们必须使用 Dapper 才不会与任何东西发生冲突否则。
所以,我可以轻松地将IdentityDbContext 和所有其他相关内容移植到EntityFrameworkCore。
但现在是迁移。我知道微软在从数据库中检索模型和 DbContext 时有一个document。但是,这会创建一个包含所有配置的非常长的 OnModelCreating 方法。看起来这可以工作但是,如果我想做任何新的更改,我必须在新的迁移中进行更改,并确保没有冲突。
此外,我们当前的迁移进行了手动调整,以确保它适用于新数据库或现有生产数据库。这种方式没有处理好一些事情。
我想要的是将现有的单个迁移文件迁移到新的代码语法。我不在乎是否省略了手动调整,但如果以某种方式自动转换最大的东西。
例如:
.NET 框架:
public override void Up()
{
CreateTable(
"dbo.AdditionalCosts",
c => new
{
additionalCostId = c.Int(nullable: false, identity: true),
shoppingCartId = c.Int(),
shoppingCartProductId = c.Int(),
orderId = c.Int(),
inclPrice = c.Decimal(nullable: false, precision: 18, scale: 3),
taxPrice = c.Decimal(nullable: false, precision: 18, scale: 3),
description = c.String(),
title = c.String(),
costType = c.Int(nullable: false),
})
.PrimaryKey(t => t.additionalCostId)
.ForeignKey("dbo.Orders", t => t.orderId)
.ForeignKey("dbo.ShoppingCartProducts", t => t.shoppingCartProductId)
.ForeignKey("dbo.ShoppingCarts", t => t.shoppingCartId)
.Index(t => t.shoppingCartId)
.Index(t => t.shoppingCartProductId)
.Index(t => t.orderId);
}
.NET 核心:
是否有可用的转换工具?
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
"dbo.AdditionalCosts",
c => new
{
additionalCostId = c.Column<int>(nullable: false), //idnetity: true?
shoppingCartId = c.Column<int>(),
shoppingCartProductId = c.Column<int>(),
orderId = c.Column<int>(),
inclPrice = c.Column<decimal>(nullable: false), //precision: 18, scale: 3?
taxPrice = c.Column<decimal>(nullable: false), //precision: 18, scale: 3
description = c.Column<string>(),
title = c.Column<string>(),
costType = c.Column<int>(nullable: false),
}, constraints: table =>
{
table.PrimaryKey("PK_AdditionalCost", t => t.additionalCostId);//default naming of PK_? why not automated anymore! :(
//etc..
});
//.PrimaryKey(t => t.additionalCostId)
//.ForeignKey("dbo.Orders", t => t.orderId)
//.ForeignKey("dbo.ShoppingCartProducts", t => t.shoppingCartProductId)
//.ForeignKey("dbo.ShoppingCarts", t => t.shoppingCartId)
//.Index(t => t.shoppingCartId)
//.Index(t => t.shoppingCartProductId)
//.Index(t => t.orderId);
}
【问题讨论】:
标签: c# entity-framework .net-core entity-framework-core .net-standard