Configure Relationship in Entity Framework using Fluent API

One-to-zero or one relationship using Fluent API

Let’s configure one-to-zero or one relationship using Customer and Customer Address entities.

Customer.cs

  1. public class Customer  
  2. {  
  3.     publicintCustomerId  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string CustomerName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string CustomerEmail  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public CustomerAddressCustomerAddr  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     publicCustomer()  
  24.     {  
  25.     }  
  26. }
CustomerAddress.cs
  1. public class CustomerAddress  
  2. {  
  3.     public int CustomerAddressId  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string Address  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string City  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public CustomerCustomer  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     public CustomerAddress()  
  24.     {  
  25.     }  
  26. }  
Now, we can pass to configure one to one or zero relationship using code as shown below:
  1. protected override void OnModelCreating(DbModelBuildermodelBuilder)  
  2. {  
  3.     // Now, I will configure Customer &CustomerAddress entities  
  4.     modelBuilder.Entity < Customer > ()  
  5.         .HasOptional(c => c.CustomerAddr)  
  6.         .WithRequired(Ad => Ad.Customer);  
  7. } 
  • HasOptional (c=>c.CustomerAddr) indicates that Address is optional for customer entity.
  • WithRequired (Ad=>Ad.Customer) indicates that Customer property is required for Customer Address entity then you can understand that you cannot save Customer Address without Customer.

Note

One-to-one is not possible in MS SQL Server. EF creates one-to-zero or one relationship on entities not in database.



At this level, I would like to share with you the following question:

If CustomerAddressId does not follow primary key convention (className<Id>), how can we do to handle this problem?

Now, let’s consider the following Customer & CustomerAddress entities.

  1. public class Customer  
  2. {  
  3.     public int CustomerID  
  4.     {  
  5.         get;  
  6.         set;  
  7.     }  
  8.     public string CustomerName  
  9.     {  
  10.         get;  
  11.         set;  
  12.     }  
  13.     public string CustomerEmail  
  14.     {  
  15.         get;  
  16.         set;  
  17.     }  
  18.     public CustomerAddressCustomerAddr  
  19.     {  
  20.         get;  
  21.         set;  
  22.     }  
  23.     public Customer()  
  24.     {  
  25.     }  
  26. }  
  27. public class CustomerAddress  
  28. {  
  29.     public int Id  
  30.     {  
  31.         get;  
  32.         set;  
  33.     }  
  34.     public string Address  
  35.     {  
  36.         get;  
  37.         set;  
  38.     }  
  39.     public string City  
  40.     {  
  41.         get;  
  42.         set;  
  43.     }  
  44.     public CustomerCustomer  
  45.     {  
  46.         get;  
  47.         set;  
  48.     }  
  49.     public CustomerAddress()  
  50.     {  
  51.     }  
  52. }  
So, to overcome this problem, we need to add line which specifies Id property is considered as primary key.
  1. protected override void OnModelCreating(DbModelBuildermodelBuilder)  
  2. {  
  3.     //Configure Id as primary key for customerAddress entity  
  4.     modelBuilder.Entity < CustomerAddress > ().HasKey(CA => CA.Id);  
  5.     modelBuilder.Entity < Customer > ()  
  6.         .HasOptional(c => c.CustomerAddr)  
  7.         .WithRequired(Ad => Ad.Customer);  
  8. }