0

I am trying to create a zero to one relationship with code first entity framework. With the sample code below, I am getting error message:

Unable to determine the principal end of an association between the types 'CompanyView' and 'MemberView'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.    

With this error, I have tried added in a configuration to fix this.

modelBuilder.Entity<CompanyView>().HasOptional(x => x.MemberView).WithOptionalPrincipal(x => x.CompanyView);

Which ended up with another error message.

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

Anyone have any idea on what is going one here? Thanks.

public class CompanyView 
{
    [Key]
    public Guid Id { get; private set; }

    public Guid? MemberId { get; private set; }

    [ForeignKey("MemberId")]
    public MemberView MemberView { get; private set; }
}

public class MemberView
{
    [Key]
    public Guid Id { get; private set; }

    public Guid? CompanyId { get; private set; }

    [ForeignKey("CompanyId")]
    public CompanyView CompanyView { get; private set; }
}

The end result I would expected is as below: A MemberView can have 0 or 1 CompanyView, each CompanyView can have 0 to 1 MemberView.

  • Possible duplicate of [Is it possible to capture a 0..1 to 0..1 relationship in Entity Framework?](http://stackoverflow.com/questions/21889367/is-it-possible-to-capture-a-0-1-to-0-1-relationship-in-entity-framework) – Gert Arnold Feb 05 '16 at 07:50
  • Tried the solution from the link, code below fix my problem. modelBuilder.Entity().HasOptional(x => x.MemberView).WithMany().HasForeignKey(x => x.MemberId); modelBuilder.Entity().HasOptional(x => x.CompanyView).WithMany().HasForeignKey(x => x.CompanyId); – Rambo Ong Ping Ping Feb 05 '16 at 17:14

1 Answers1

0

Relationship as you specified is unusual (one or zero at both directions). You try to create foreign keys at both tables, as the result you have insoluble loop. You can easily create relation one or zero in case when one entity is principal and one is dependent. You can try to simulate desired relation via many to many relationship. Also you can to not annotate fields with foreign key attributes, as the result you will not have some benefits, but you will be able to flexible manipulate relationships manually on your own. At the end, I demonstrate relation where, for example, MemberView is dependent table:

public class CompanyView
{
    [Key]
    public Guid Id { get; private set; }        
    public MemberView MemberView { get; private set; }
}

public class MemberView
{        
    [Key, ForeignKey("CompanyView")]
    public Guid CompanyId { get; private set; }        
    public CompanyView CompanyView { get; private set; }
}
Slava Utesinov
  • 13,410
  • 2
  • 19
  • 26