实体框架代码首先是一对一的关系 - 不同的行为

时间:2015-05-06 07:24:25

标签: c# entity-framework entity-framework-5 entity-framework-6

我有2个对象,它们以一对一的关系绑定到3-d:

public class Project : AuditInfoComplete
{
    [Key]
    [DataMember]
    public Guid ProjectId { get; set; }

    [DataMember]
    public virtual Ecwid Ecwid { get; set; }

    [DataMember]
    public virtual ProjectDomain ProjectDomain { get; set; }
}

[DataContract(IsReference = true)]
public class Ecwid
{
    [Key, ForeignKey("Project")]
    [DataMember]
    public Guid ProjectId { get; set; }

    [DataMember]
    public string EcwidShopID { get; set; }

    public virtual Project Project { get; set; }
}

[DataContract(IsReference = true)]
public class ProjectDomain
{
    [Key, ForeignKey("Project")]
    public Guid ProjectId { get; set; }

    [DataMember]
    public string Domain { get; set; }

    public virtual Project Project { get; set; }
}

在DbContext中我有这个:

        modelBuilder.Entity<Ecwid>().HasKey(p => p.ProjectId);
        modelBuilder.Entity<Project>().HasOptional(p => p.Ecwid).WithRequired(p => p.Project).WillCascadeOnDelete();

        modelBuilder.Entity<ProjectDomain>().HasKey(p => p.ProjectId);
        modelBuilder.Entity<Project>().HasOptional(p => p.ProjectDomain).WithRequired(p => p.Project).WillCascadeOnDelete();

当我创建Project时 - 在数据库中为我自动创建“Ecwid”对象,但“Project Domain”对象没有创建。为什么行为不同?

0 个答案:

没有答案