外键约束问题代码首先

时间:2014-12-12 08:29:14

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

我首先尝试使用代码为Collection类创建父子关系,但是我收到以下错误:

  

引入FOREIGN KEY约束   表格' subcollect'上的FK.dbo.subcollect.dbo.collection_subcollectionID   可能导致循环或多个级联路径

我认为添加' WillCascadeOnDelete(false)'会解决这个问题,但这并没有奏效。我觉得这与循环引用有关,但我不确定......

主要集合类:

public class Collection
{
    public int CollectionID { get; private set; }    

    public ICollection<SubCollect> SubCollects { get; private set; }
}

子收集类:

public class SubCollect
{
    public int SubCollectID { get; private set; }

    public int? ParentCollectionID { get; private set; }
    public Collection ParentCollection { get; private set; }

    public int? SubCollectionID { get; private set; }
    public Collection SubCollection { get; private set; }

    public bool IsFeatured { get; private set; }
    public int Position { get; private set; }
}

这是配置类:

public class SubCollectConfiguration: EntityTypeConfiguration<SubCollect>
{
    public SubCollectConfiguration()
    {
        this.HasRequired(c => c.ParentCollection)
           .WithMany(c => c.SubCollects)
           .HasForeignKey(c => c.ParentCollectionID)
           .WillCascadeOnDelete(false);
        this.HasRequired(c => c.SubCollection)
                               .WithMany()
                               .HasForeignKey( c => c.SubCollectionID)
                                  .WillCascadeOnDelete(false);
    }
}

0 个答案:

没有答案