我有类似的结构,如下所示。
public class Price
{
public decimal Amount {get; set;}
public string Currency {get; set;}
}
public class Product : BaseEntity
{
public int Id {get; set;}
public string Name {get; set;}
public Price Price {get; set;}
}
我想要数据库中的产品表将价格与其属性分开。例如;
**ProductTable**
--Id
--Name
--Amount
--Currency
然后,当我从数据库中获取产品时,它会自动将金额和货币绑定(bind)到产品<的价格对象/强>。我应该如何使用 Entity Framework Code First 来制作这个结构?
最佳答案
价格属性必须是虚拟的,并且应该有效:
public class Product
{
public int Id {get; set;}
public string Name {get; set;}
public virtual Price Price {get; set;}
}
编辑:
您可以通过重写 DbContext
类中的 OnModelCreating
方法来指定自定义列名称:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Product>().Property(u => u.Price.Amount)
.HasColumnName("Amount");
}
关于c# - 代码优先模型映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15986449/