0

我有一个成员模型,其地址采用一对一样式,其中地址行只能属于一个成员。我试图弄清楚如何定义它,以便成员拥有地址,并且当我删除成员时,地址被级联删除。

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();

当我有机会时,我会发布它是否有效

4

2 回答 2

0

Your logic is twisted upside down. What you've written now is that each member has an address, which results in a fact that one address can be present for several members, meaning that in hierarchy table, address is above the member. Deleting a child can't result in deleting a parent.

Try deleting the address and your member(s) will be gone, if you want to test what I just wrote.

For your logic to work, you need to write:

public class Member
{
    [Key]
    public int Id { get; set; }
...
}

public class Address : IEntity
{
    [Key]
    public int Id { get; set; }

    public int MemberId {get;set;}
    public virtual Member Member {get;set;}
    ....
}

If you turn on cascade delete, and delete member, address will be deleted as well.

EDIT:

If you're looking for 1 to 1 relationship, it goes like this:

public class Member
{
[Key]
public int Id {get;set;}

...

public virtual Address Address {get;set;}
}

public class Address
{
[ForeignKey("Member")]
public int Id {get;set;}

...

public virtual Member Member {get;set;}
}
于 2013-09-16T18:30:43.217 回答
0

我尝试了一些不同的东西,但这是 EF 创建一对一映射的方式,只有在关系的一侧有导航..

public class Member
{
    [Key]
    public int Id { get; set; }

    public virtual Address Address { get; set; }
}

public class Address
{
    [Key]
    public int MemberId {get;set;}
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder
        .Entity<Address>()
        .HasKey(t => t.MemberID);

    modelBuilder
        .Entity<Member>()
        .HasRequired(t => t.Address)
        .WithRequiredPrincipal();
}

这样做可以使级联删除正常工作,以便在删除成员时删除地址。

于 2013-09-18T16:51:12.780 回答