# EF 0..1 relationships

Published: 2015-10-17
Last updated: 2019-11-17

It's the second time I'm seeing the following error this week and I thought I should document it:

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

I got the above error while adding an EF migration for the following models:

```java
public class A
{
    public int Id { get; set; }
    public string SomeField { get; set; }
    public virtual B B { get; set; }
}

public class B
{
    public int Id { get; set; }
    [ForeignKey("A")]
    public int? AId { get; set; }
    public virtual A A { get; set; }
}

```

In the above example, `A` and `B` are in a one-to-many relationship. So the navigation property in `A` should be udpated to `public virtual ICollection<B> Bs { get; set; }`.

However what I actually intended to do was have a one to zero-or-one relationship between `B` and `A`. So the proper fix would be to make `B` the parent and `A` the child. Or in EF terms, make `B` the principal end and `A` the dependent end.

```java
public class B
{
    public int Id { get; set; }
    public virtual A A { get; set; }
}

public class A
{
    [Key, ForeignKey("B")]
    public int Id { get; set; }
    public string SomeField { get; set; }
    public virtual B B { get; set; }
}

```

Whenever a one-to-one (or a one to zero-or-one) relationship is desired the dependent end's foreign key should be its primary key as well.

Oh and BTW, a pure one-to-one relationship [doesn't really exist](http://stackoverflow.com/a/10292449).

---

Update Oct 2017: Here's a related post I wrote this Valentines day: [Notes on database relationships](https://gldraphael.com/blog/notes-on-database-relationships/).

