Coder Perfect

In the Entity Framework, the principal end of an association denotes a 1:1 relationship.

Problem

public class Foo
{
    public string FooId{get;set;}
    public Boo Boo{get;set;}
}


public class Boo
{
    public string BooId{get;set;}
    public Foo Foo{get;set;}
}

When I tried to implement this with Entity Framework, I got the following error:

I’ve read queries on StackOverflow that provide a solution for this problem, but I’m not sure what “principal end” implies.

Asked by taher chhabrawala

Solution #1

One end of a one-to-one relationship must be the principal, while the other must be the dependant. The primary end is the one that will be placed first and can exist independently of the dependent end. Because it has a foreign key to the principal, the dependent end must be inserted after the principal.

In the case of entity framework, the FK in the dependant must also be the PK, hence in your situation, use:

public class Boo
{
    [Key, ForeignKey("Foo")]
    public string BooId{get;set;}
    public Foo Foo{get;set;}
}

Or fluent mapping

modelBuilder.Entity<Foo>()
            .HasOptional(f => f.Boo)
            .WithRequired(s => s.Foo);

Answered by Ladislav Mrnka

Solution #2

To solve this, you may alternatively utilize the [Required] data annotation attribute:

public class Foo
{
    public string FooId { get; set; }

    public Boo Boo { get; set; }
}

public class Boo
{
    public string BooId { get; set; }

    [Required]
    public Foo Foo {get; set; }
}

Boo necessitates the use of Foo.

Answered by Leniel Maccaferri

Solution #3

This is in response to @Ladislav Mrnka’s response about configuring one-to-one relationships with fluent api.

It was not possible in a case when the dependent’s FK had to be its PK.

For example, Foo and Bar already have a one-to-many relationship.

public class Foo {
   public Guid FooId;
   public virtual ICollection<> Bars; 
}
public class Bar {
   //PK
   public Guid BarId;
   //FK to Foo
   public Guid FooId;
   public virtual Foo Foo;
}

Another one-to-one relationship between Foo and Bar had to be added now.

public class Foo {
   public Guid FooId;
   public Guid PrimaryBarId;// needs to be removed(from entity),as we specify it in fluent api
   public virtual Bar PrimaryBar;
   public virtual ICollection<> Bars;
}
public class Bar {
   public Guid BarId;
   public Guid FooId;
   public virtual Foo PrimaryBarOfFoo;
   public virtual Foo Foo;
}

Here’s how you use fluent api to specify a one-to-one relationship:

modelBuilder.Entity<Bar>()
            .HasOptional(p => p.PrimaryBarOfFoo)
            .WithOptionalPrincipal(o => o.PrimaryBar)
            .Map(x => x.MapKey("PrimaryBarId"));

It’s worth noting that we need to remove PrimaryBarId when adding it because we’re using the fluent api to define it.

It’s also worth noting that the method name [WithOptionalPrincipal()][1] is ironic. Principal is Bar in this case. The msdn explanation of WithOptionalDependent() clarifies the situation.

Answered by Sudarshan_SMD

Post is based on https://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr