public class Purchase {
public Address to { get; set; }
public Address from { get; set; }
}
public class Address {
public string name { get; set; }
}
我有1购买2地址。这应该如何在数据库(mySql)上,包括在实体框架上使用的外键。
我有一个问题,实体理解(基于fk)地址中的可导航性是1到多(*),我没有购买地址的列表,我定义了2。
谢谢,巴特。
可以配置表之间的关系。像这样构建你的模型,
public class PurchaseConfiguration : EntityTypeConfiguration<Purchase >
{
public PurchaseConfiguration()
{
HasRequired(p=>p.to ).WithOptionalDependent().WillCascadeOnDelete(false);
HasRequired(p => p.from ).WithOptionalDependent().WillCascadeOnDelete(false);
}
}
和你的数据库上下文,你可以添加这样的配置,
public class yourDbContext:DbContext
{
public DbSet<Purchase> Purchases{ get; set; }
public DbSet<Address> Addresses{ get; set; }
//other db sets here..
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new PurchaseConfiguration ());
// you can add configurations for other tables
}
}