0

Assuming I have a child

public class Child
{
    public int Id {get; set;}
}

and two parents which using the childs in one to one relations. One child should be used only at one parent. But I want to use the child with Id=1 at ParentA and the child with Id=2 at ParentB for example.

public class ParentA
{
    public int Id {get; set;}
    public int ChildId {get; set;}
    public Child Child {get; set;}
}

public class ParentB
{
    public int Id {get; set;}
    public int ChildId {get; set;}
    public Child Child {get; set;}
}

I want the navigation property at the parents if possible.

I know how to configure a one to one relation when having only one parent because then I would have to add a navigation property in the Child class and would add the configuration in the OnModelCreating method of my DbContext implementation.

But for the scenario with 2+ parents I don't know how the configuration in OnModelCreating should look like.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Mighty Badaboom
  • 6,067
  • 5
  • 34
  • 51
  • This would not be a one to one relation any more, you would need to add a `List` navigation property in the `Child` class and use a `.WithMany` within `OnModelCreating` for the `Child` class. – Etheraex Jun 10 '22 at 10:28
  • @Etheraex But I don't want a child to be used at different parents. And I don't want to add navigation properties at the child because then I would have to add a navigation property to each parent (ParentA and ParentB) and only one would be set. – Mighty Badaboom Jun 10 '22 at 10:32
  • Looking at the question again, is it possible that you made a typo in the code area for the two clases. Instead of having ParentA and ParentB classes you have 2 ParentA classes. This is kind of making it confusing. Because if these are differente classes you esentially want 2 Parent classes with 2 one-to-one relations with Child? – Etheraex Jun 10 '22 at 10:46
  • @Etheraex Ah...damn...yes typo. Changed it – Mighty Badaboom Jun 10 '22 at 10:58
  • Ok, that makes it easier to understand but this is not quite a straightforward solution. Are `ParentA` and `ParentB` both mapped to the same table? How is your DB set up? Are you familiar with any of the techniques of modeling Object inheritance in SQL tables: https://stackoverflow.com/questions/3579079/how-can-you-represent-inheritance-in-a-database – Etheraex Jun 10 '22 at 11:05
  • Haven't noticed the typo. Damn copy/paste. They are not mapped to the same table. Just make the sample as simple as possible. Never have used object inheritance but will have a look at it. thx :) – Mighty Badaboom Jun 10 '22 at 11:11

1 Answers1

0

Based on the additional info from the comments that the ParentA and ParentB are not mapped to the same table you could implement 2 one-to-one relationships resulting in:

public class Child
{
    public int Id {get; set;}
    public ParentA A {get; set;}
    public ParentB B {get; set;}
}

This would mean that when A is initialized B is null and vice versa.

Alternatively if ParentA and ParentB are similar enough and it makes sence to have a common inheritance:

public class Child
{
    public int Id {get; set;}
    public Parent {get; set;}
}

public class Parent
{
    public int Id {get; set;}
    public int ChildId {get; set;}
    public Child Child {get; set;}
}

public class ParentA : Parent { }

public class ParentB : Parent { }

And have the one-to-one relationship mapped between Parent and Child

This might also require DB changes.

Etheraex
  • 508
  • 6
  • 17
  • That was my first thought but I have more then 2 parent tables in my application and I don't want to put all the navigation properties into my child :( – Mighty Badaboom Jun 10 '22 at 11:24