Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First - 1-to-1 Optional Relationship

I want to map an optional 1-to-1 relationship in an existing database with EF Code First.

Simple schema:

User  Username  ContactID  Contact  ID  Name 

Obviously ContactID joins to Contact.ID. The ContactID field is nullable so the relationship is optional - 0 or 1, never many.

So how do I specify this relationship in EF Code First, with this existing schema?

Here's what I've tried so far:

public class User {     [Key]     public string Username { get; set; }     public int? ContactID { get; set; }      [ForeignKey("ContactID")]     public virtual Contact Contact { get; set; } }  public class Contact {     [Key]     public int ID { get; set; }     public string Name { get; set; }      public virtual User User { get; set; } }  modelBuilder.Entity<User>().HasOptional<Contact>(u=> u.Contact)     .WithOptionalDependent(c => c.User); 

I get the following Exception:

  System.Data.Edm.EdmAssociationEnd: : Multiplicity is not valid in Role  'User_Contact_Source' in relationship 'User_Contact'. Because the Dependent  Role properties are not the key properties, the upper bound of the multiplicity  of the Dependent Role must be *. 
like image 997
Chris Moschini Avatar asked May 16 '11 17:05

Chris Moschini


People also ask

How do I map a one-to-many relationship in Entity Framework?

“HasMany” and “WithMany” method is used to define one-to-many or many-to-many relation in entity framework. We can configure one-to-many relationships between People and PeopleAddress using Fluent API by the following code in the model class: protected override void OnModelCreating(DbModelBuildermodelBuilder) {

How do I create a one-to-many relationship in Entity Framework database first?

Adding an Author navigation property will create a one-to-many relationship between the Authors and Books tables in the database by adding a foreign key Author_AuthorId to Books table.

How do you create a foreign key relationship in code first approach?

To create Foreign Key, you need to use ForeignKey attribute with specifying the name of the property as parameter. You also need to specify the name of the table which is going to participate in relationship. I mean to say, define the foreign key table. Thanks for reading this article, hope you enjoyed it.

How will you create relationship between tables in Entity Framework?

You can create such a relationship by defining a third table, called a junction table, whose primary key consists of the foreign keys from both table A and table B.


1 Answers

One solution would be;

public class User {     [Key]     public string Username { get; set; }      public virtual Contact Contact { get; set; } }  public class Contact {     [Key]     public int ID { get; set; }     public string Name { get; set; }      public virtual User User { get; set; } }          modelBuilder.Entity<User>()             .HasOptional<Contact>(u => u.Contact)             .WithOptionalDependent(c => c.User).Map(p => p.MapKey("ContactID")); 

You set only your navigational objects in your POCOs and instead you use fluent API to map your key to the correct column.

like image 81
Per Avatar answered Sep 27 '22 17:09

Per