0

I have 3 classes A, B and AB. Both class A and class B are independent on each other. But AB depends on A and B. I would like to achieve a " Zero or One to Zero or One" relationship between A and B using Entity Framework code-first.

Can someone tell me why this doesn't work? Or am I completely wrong? Thank you!

public class A
{       
    public int Id { get; set; }
    public string PropertyName { get; set; }

    public virtual AB AB { get; set; }        
}

public class B
{       
    public int Id { get; set; }
    public string PropertyName { get; set; }

    public virtual AB AB { get; set; }        
}

public class AB
{
    [Key, ForeignKey("A"), Column(Order = 0)]
    public int AId { get; set; }

    [Key, ForeignKey("B"), Column(Order = 1)]
    public int BId { get; set; }

    public virtual A A { get; set; }

    public virtual B B { get; set; }
}

I'm getting this error:

AB_B_Source: : Multiplicity is not valid in Role 'AB_B_Source' in relationship 'AB_B'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

AB_A_Source: : Multiplicity is not valid in Role 'AB_A_Source' in relationship 'AB_A'. Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.

1 Answers1

1

Based on the edited model, the navigation properties AB in each class (A and B) could be the problem because to point to an AB object you would need two keys at the same time. Entity AB is probably mapped with a compound primary key and the relation you want should be already defined with the properties A and B in that entity.

Let me know if it worked, Have a nice day

Mr.Matt
  • 151
  • 2
  • 5
  • Thank you for your answer. Indeed I've been trying to create a simplified model from the existing more complex one, and I omitted to remove some parts. The class `GranteeFingerPrintInfo` is actually `AB` and the annotation over the property `Id` in class `A` should be removed. So the annotation is not a problem in the real model. Please consider the changes, I edited mu question. Thank you! – Edinio DESIR Sep 04 '17 at 02:03