0

I have two classes ElementData and Socket.

  • An ElementData can have multiple Sockets
  • A Socket can have an ElementData

So you can nest a series of ElementData.

The classes look like this:

public class ElementData
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Type { get; set; } = string.Empty;
    public string Category { get; set; } = string.Empty;

    public List<SocketData>? Sockets { get; set; } = new List<SocketData>();
}

public class SocketData
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Alias { get; set; } = string.Empty;

    public int ParentElementId { get; set; }
    public ElementData ParentElement { get; set; } = new ElementData();

    public int ChildElementId { get; set; }
    public ElementData ChildElement { get; set; } = new ElementData();

    public string ValidElementTypes { get; set; } = string.Empty;
}

But if I create a migration, it just connects the ChildElement to the parent element and only create ChildElementId row in the database.

I was hoping for a database table with columns

[Id], [Alias], [ParentElementId], [ChildElementId], [ValidElementTypes]

where ParentElement has a foreign key to parent ElementData, and the ChildElement has a foreign key to child ElementData.

How do I accomplish this?

Thanks in advance :-)

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
MüllerDK
  • 256
  • 4
  • 12
  • I think one thing that could trip it up is that you have two ElementData fields in SocketData, but only one Sockets list in ElementData, so it cannot figure out how to connect them. Since this is not a simple case following the conventions, you should [configure the relationships manually](https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key#manual-configuration). – molnarm Jan 19 '23 at 13:58
  • Side notes: (1) Never do this `= new ElementData();` with reference navigation properties (2) never do this `List? Sockets` with collection navigation properties (they can be empty, but not null). As for the concrete question, every reference navigation property must be mapped to a **separate* collection navigation property, because these are separate relationships. In your case, you have 2. The whole question is duplicate unrelated to EFC 7. – Ivan Stoev Jan 19 '23 at 14:01
  • @IvanStoev How should I modify to get it working.. Not quite following :-| – MüllerDK Jan 19 '23 at 14:11
  • @MüllerDK Create 2 `List` properties in `ElementData`. Name them as you wish (one could be the currently existing `Sockets` property). Then, using fluent API, map one of the collections to the `SocketData.ParentElement`, and the second one to the `SocketData.ChildElement`. Note that this effectively creates many-to-many self relationship between `ElementData`, with `SocketData` serving as a join entity/table. Clear? :-) Basically the same as in the linked "duplicate", just different class/property names. – Ivan Stoev Jan 19 '23 at 16:48

0 Answers0