我在az deployment create \
-n demoEmptyRG \
-l southcentralus \
--template-uri https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/azure-resource-manager/emptyRG.json \
--parameters rgName=demoRG rgLocation=northcentralus
DbContext
但是当我插入几个订单时,每次都使用相同的代码:
protected override void OnModelCreating(ModelBuilder builder)
{
builder.Entity<Order>().HasAlternateKey(o => o.OrderNumber);
base.OnModelCreating(builder);
}
即使它们都具有相同的var order = new Order()
{
OrderNumber = "12345",
OrderParts = new List<OrderPartQuantity>()
{
new OrderPartQuantity()
{
Part = _context.Parts.FirstOrDefault(p => p.BellNumber == "100123"),
Quantity = 10
}
}
};
_context.Orders.Add(Order);
_context.SaveChanges();
值,每次也会插入该订单。
我是否理解不正确,或者我缺少什么?我正在为此寻找一个解释
答案 0 :(得分:1)
因此,似乎在HasAlternateKey
中调用OnModelBuilding
实际上需要进行新的数据库迁移。
致电后
Add-Migration TestHasAlternateKeys
此迁移已创建
public partial class HasAlternateKeyTest : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<string>(
name: "OrderNumber",
table: "Orders",
nullable: false,
oldClrType: typeof(string),
oldNullable: true);
migrationBuilder.AddUniqueConstraint(
name: "AK_Orders_OrderNumber",
table: "Orders",
column: "OrderNumber");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropUniqueConstraint(
name: "AK_Orders_OrderNumber",
table: "Orders");
migrationBuilder.AlterColumn<string>(
name: "OrderNumber",
table: "Orders",
nullable: true,
oldClrType: typeof(string));
}
}
现在,致电后
Update-Database
我不能再重复该列