0

Ich habe eine Klasse, Beziehung, die eine InverseRelationship-Eigenschaft haben wird. Jede gegebene Beziehung wird immer genau eine InverseRelation haben, die auch eine Beziehung ist.Wie erstellen Sie eine selbstverweisende Entität in Entity Framework Core?

Meine Klasse sieht wie folgt jetzt:

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 
} 

Wenn ich versuche die Tabelle zu füllen, erhalte ich eine SMS-Nachricht:

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

So dann habe ich versucht, Fluent API zu verwenden, um dies zu tun , und ich endete mit dem folgenden:

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

Und wenn ich versuche, eine Migration zu erstellen, bekomme ich Folgendes:

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'. 

Das macht Sinn, aber ich weiß nicht, wie ich es umgehen soll. Ich weiß nicht, wie ich erreichen kann, wofür ich hier bin.

Aktualisieren Hier ist der Code, den ich verwendet habe, um die Tabelle tatsächlich zu füllen (vor dem Versuch, Model Builder-Code zu verwenden).

 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

die Diskussion und Satz hier könnte nützlich sein: https://github.com/aspnet/EntityFramework/issues/3376 – caesay

+1

Können Sie Code schreiben, wie sparen Sie Entitäten? – Smit

+0

@Smit Ich habe es zu der ursprünglichen Nachricht. Ich hatte nicht bedacht, dass das ist, wo das Problem sein könnte, aber es macht Sinn, dass es jetzt könnte sein, dass Sie es erwähnen. – LayfieldK

Antwort

1

Dank @Smit habe ich herausgefunden, was los ist.

Offenbar müssen Sie

DbContext.SaveChanges(); 

Vor ‚Schließen‘ den Kreis in dem Kreis Bezug rufen. Unten ist funktionierender Code.

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