2017-03-21 98 views
0

我有一個類,關係,將有一個InverseRelationship屬性。任何給定的關係總是隻有一個InverseRelationship,這也是一個關係。如何在實體框架核心中創建自引用實體?

我的類看起來像現在這樣的權利:

public class Relationship 
{ 
    #region Constructor 
    public Relationship() 
    { 

    } 
    #endregion Constructor 

    #region Properties 
    [Key] 
    [Required] 
    public int Id { get; set; } 
    [Required] 
    public string Description { get; set; } 
    public int? InverseRelationshipId { get; set; } 

    #endregion Properties 

    #region Related Properties 
    [ForeignKey("InverseRelationshipId")] 
    public virtual Relationship InverseRelationship { get; set; } 

    #endregion Related Properties 
} 

當我嘗試填充表,我得到一個消息說

Unable to save changes because a circular dependency was detected in the data to be saved 

於是我試圖用流利的API來做到這一點,我結束了以下內容:

modelBuilder.Entity<Relationship>() 
      .HasOne(r => r.InverseRelationship) 
      .WithOne(r => r.InverseRelationship) 
      .HasForeignKey<Relationship>(r => r.InverseRelationshipId) 
      .IsRequired(false); 

而當我嘗試創建遷移時,我得到以下內容:

The navigation property 'InverseRelationship' cannot be added to the entity type 'Relationship' because a navigation property with the same name already exists on entity type 'Relationship'. 

這是有道理的,但我不知道如何解決它。我對如何實現我在這裏的目標感到茫然。

更新 這是我用來實際填充表的代碼(在嘗試使用模型構建器代碼之前)。

 EntityEntry<Relationship> rel1 = DbContext.Relationships.Add(new Relationship() 
    { 
     UserId = authorId, 
     Title = "Relationship 1", 
     Description = "Relationship 1", 
     CreatedDate = createdDate, 
     LastModifiedDate = lastModifiedDate 
    }); 

    EntityEntry<Relationship> rel2 = DbContext.Relationships.Add(new Relationship() 
    { 
     UserId = authorId, 
     Title = "Relationship 2", 
     Description = "Relationship 2", 
     CreatedDate = createdDate, 
     LastModifiedDate = lastModifiedDate, 
     InverseRelationship = rel1.Entity 
    }); 

    EntityEntry<Relationship> rel3 = DbContext.Relationships.Add(new Relationship() 
    { 
     UserId = authorId, 
     Title = "Relationship 3", 
     Description = "Relationship 3", 
     CreatedDate = createdDate, 
     LastModifiedDate = lastModifiedDate 
    }); 

    EntityEntry<Relationship> rel4 = DbContext.Relationships.Add(new Relationship() 
    { 
     UserId = authorId, 
     Title = "Relationship 4", 
     Description = "Relationship 4", 
     CreatedDate = createdDate, 
     LastModifiedDate = lastModifiedDate, 
     InverseRelationship = rel3.Entity 
    }); 

    rel1.Entity.InverseRelationship = rel2.Entity; 
    rel3.Entity.InverseRelationship = rel4.Entity; 

    DbContext.SaveChanges(); 
+0

討論,並主張在這裏可能是有用的:https://github.com/aspnet/EntityFramework/issues/3376 – caesay

+1

你能後的代碼你是如何保存實體? – Smit

+0

@Smit我將它添加到原始文章。我沒有考慮到這就是問題所在,但現在可以提到它是有道理的。 – LayfieldK

回答

1

感謝@Smit,我想出了發生了什麼事。

顯然,你需要調用

DbContext.SaveChanges(); 

「關閉」的循環引用前圈。以下是工作代碼。

EntityEntry<Relationship> rel1 = DbContext.Relationships.Add(new Relationship() 
{ 
    UserId = authorId, 
    Title = "Relationship 1", 
    Description = "Relationship 1", 
    CreatedDate = createdDate, 
    LastModifiedDate = lastModifiedDate 
}); 

EntityEntry<Relationship> rel2 = DbContext.Relationships.Add(new Relationship() 
{ 
    UserId = authorId, 
    Title = "Relationship 2", 
    Description = "Relationship 2", 
    CreatedDate = createdDate, 
    LastModifiedDate = lastModifiedDate, 
    InverseRelationship = rel1.Entity 
}); 

EntityEntry<Relationship> rel3 = DbContext.Relationships.Add(new Relationship() 
{ 
    UserId = authorId, 
    Title = "Relationship 3", 
    Description = "Relationship 3", 
    CreatedDate = createdDate, 
    LastModifiedDate = lastModifiedDate 
}); 

EntityEntry<Relationship> rel4 = DbContext.Relationships.Add(new Relationship() 
{ 
    UserId = authorId, 
    Title = "Relationship 4", 
    Description = "Relationship 4", 
    CreatedDate = createdDate, 
    LastModifiedDate = lastModifiedDate, 
    InverseRelationship = rel3.Entity 
}); 

DbContext.SaveChanges(); 

rel1.Entity.InverseRelationship = rel2.Entity; 
rel3.Entity.InverseRelationship = rel4.Entity; 

DbContext.SaveChanges();