考慮以下模型:
class TaxRate : Entity
{
public string Code { get; private set; }
}
class Money : ValueObject
{
public decimal Amount { get; private set; }
}
class Price : ValueObject
{
public Money Net { get; private set; }
public Money? Tax { get; private set; }
public TaxRate? TaxRate { get; private set; }
}
class Product : Entity
{
public Price Price { get; private set; }
}
使用以下配置
class ProductMap
{
builder.OwnsOne<Price>(p => p.Price, b =>
{
b.OwnsOne<Money>(p => p.Net, b =>
b.Property(p => p.Amount).HasColumnName("Net").IsRequired());
b.OwnsOne<Money>(p => p.Tax, b =>
b.Property(p => p.Amount).HasColumnName("Tax").IsRequired(false));
b.HasOne<TaxRate>(p => p.TaxRate)
.WithMany()
.HasForeignKey("TaxRateId");
}).Navigation(p => p.Price).IsRequired();
}
我的產品表在遷移中生成了以下欄位:
Net = table.Column<decimal>(type: "decimal(12,2)", nullable: false),
Tax = table.Column<decimal>(type: "decimal(12,2)", nullable: true),
Price_TaxRateId = table.Column<int>(type: "int", nullable: true),
如何將“Price_TaxRateId”列創建為“TaxRateId”?
uj5u.com熱心網友回復:
HasForeignKey指定 FK(陰影)屬性名稱。然而,關聯的列名仍按約定生成并為其添加前綴。
您需要額外顯式配置列名 - 像往常一樣使用HasColumnName,例如
b.HasOne<TaxRate>(p => p.TaxRate)
.WithMany()
.HasForeignKey("TaxRateId");
b.Property("TaxRateId").HasColumnName("TaxRateId"); // <--
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426965.html
