我有一个成员模型,其地址采用一对一样式,其中地址行只能属于一个成员。我试图弄清楚如何定义它,以便成员拥有地址,并且当我删除成员时,地址被级联删除。
public class Member
{
[Key]
public int Id { get; set; }
[Required]
[ForeignKey("Address")]
public int AddressId { get; set; }
public virtual Address Address { get; set; }
}
我也有地址
public class Address : IEntity
{
[Key]
public int Id { get; set; }
....
}
我尝试了以下方法来获取级联删除的地址
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Entity<Member>()
.HasRequired(m => m.Address)
.WithRequiredDependent()
.WillCascadeOnDelete(true);
}
和
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder
.Entity<Member>()
.HasRequired(m => m.Address)
.WithRequiredPrinciple()
.WillCascadeOnDelete(true);
}
有人可以在这里帮助我走上正轨吗?迁移失败并显示以下行。
System.Data.Entity.ModelConfiguration.ModelValidationException: One
or more validation errors were detected during model generation:
System.Data.Entity.Edm.EdmAssociationEnd: : Multiplicity is not valid
in Role 'Member_Address_Source' in relationship 'Member_Address'. Because the
Dependent Role properties are not the key properties, the upper bound of the
multiplicity of the Dependent Role must be '*'.
从文档中找到这个- 一对一只有一个导航属性
modelBuilder.Entity<OfficeAssignment>()
.HasKey(t => t.InstructorID);
modelBuilder.Entity<Instructor>()
.HasRequired(t => t.OfficeAssignment)
.WithRequiredPrincipal();
当我有机会时,我会发布它是否有效