I am trying to store a set of nodes that have references to other nodes using Entity Framework Core. Each node can reference X number of nodes. Let's use the following graph as an example.
Below is a map of which nodes go with.
A -> B
B -> C
C -> B
C -> A
C -> D
D -> C
Here is my setup in Entity Framework Core. I am using a List of Locations
public class Location { public Guid Id { get; set; } public string Name { get; set; } public List<Location> Locations { get; set; } } I then map A to B. Everything goes well. But when I try to reference B to A it produces a circular reference error. Here a and b are both Location types.
a.Locations.Add(b); context.SaveChanges(); b.Locations.Add(a); context.SaveChanges(); // Circular Reference Error Here I think using the Fluent API holds the key. I've tried the following but that leads to an error about a missing key but I don't have a way to add a key. Here is my attempt.
modelBuilder.Entity<Location>(e => { e.HasMany(l => l.Locations) .WithMany(l => l.Locations); }); How do I implement a circular many to many relationship as described?
https://stackoverflow.com/questions/66631153/how-do-i-implement-a-circular-reference-in-entity-core-framework-5 March 15, 2021 at 09:03AM
没有评论:
发表评论