OnModelCreating

时间:2013-02-22 06:54:24

标签: c# entity-framework ef-code-first entity-framework-5

我有三个课程:AddressUserApplicationUserApplication都有一个地址字段。

用户:

    [Required, ForeignKey("HomeAddress")]
    public int HomeAddressId { get; set; }
    public virtual Address HomeAddress { get; set; }

应用:

    [Required, ForeignKey("PresentAddress")]
    public int PresentAddressId { get; set; }
    public virtual Address PresentAddress { get; set; }

我正在编写以下代码来解决“循环或多个级联路径”问题。我想删除Application的级联删除。以下代码是否正确?

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Application>()
            .HasRequired(x => x.PresentAddress)
            .WithMany()
            .HasForeignKey(x => x.PresentAddressId)
            .WillCascadeOnDelete(false);
    }

0 个答案:

没有答案