Reducing Complexity Using Entity Framework Core Owned Types

I came across a very nice feature of Entity Framework Core that I would like to share with you. It is the owned types.
 
EF Core’s owned types allow you to group fields that you do not want to appear as a reference, in a separate type.
 
Let us start with an example. Suppose you have this model. You have Customer and Order and both have fields for an address.
  1. class Customer  
  2. {  
  3.   [Key]  
  4.   public long CustomerID { getset; }  
  5.   public string Name { getset; }  
  6.   
  7.   public string Street { getset; }  
  8.   public string City { getset; }  
  9.   public string State { getset; }  
  10. }  
  11. class Order  
  12. {  
  13.   [Key]  
  14.   public long OrderID { getset; }  
  15.   public bool IsShipped { getset; }  
  16.   
  17.   public string Street { getset; }  
  18.   public string City { getset; }  
  19.   public string State { getset; }  
  20. }  
You can easily refactor the previous model by extracting the address fields to a new table and reference that table in your Customer and Order tables.
  1. class Address  
  2. {  
  3.   [Key]  
  4.   public long AddressID { getset; }  
  5.   public string Street { getset; }  
  6.   public string City { getset; }  
  7.   public string State { getset; }  
  8. }  
But what if you want to keep the three address fields inside Customer and Order, and you do want to refactor the code and reduce complexity and duplication, what you are going to do? The answer is in owned entities.
 
You can update your Address class and remove its key field and include a reference in Customer and Order as following,
  1. class Address  
  2. {  
  3.   public string Street { getset; }  
  4.   public string City { getset; }  
  5.   public string State { getset; }  
  6. }  
  7. class Customer  
  8. {  
  9.   [Key]  
  10.   public long CustomerID { getset; }  
  11.   public string Name { getset; }  
  12.   
  13.   public Address Address { getset; }  
  14. }  
  15. class Order  
  16. {  
  17.   [Key]  
  18.   public long OrderID { getset; }  
  19.   public bool IsShipped { getset; }  
  20.   
  21.   public Address Address { getset; }  
  22. }  
Finished? Not yet. You cannot leave your code like this as no entity can be defined without a primary key. Actually, you will get this error if you try to apply your changes to the database.
 
 
The next update that we need to do is in the data context itself. We can use the Fluent API to give a hint of how our model will work.
  1. protected override void OnModelCreating(ModelBuilder modelBuilder)  
  2. {  
  3.   base.OnModelCreating(modelBuilder);  
  4.   
  5.   modelBuilder.Entity<Customer>().OwnsOne(a => a.Address);  
  6.   modelBuilder.Entity<Order>().OwnsOne(a => a.Address);  
  7. }  
Now we can apply our model to the database and watch the magic happen!
 
 
Our update looks very promising; however, we have a very little issue here, no one likes those “Address_” prefixes!
 
The answer is in Fluent API again.
  1. protected override void OnModelCreating(ModelBuilder modelBuilder)  
  2. {  
  3.   base.OnModelCreating(modelBuilder);  
  4.   
  5.   modelBuilder.Entity<Customer>().OwnsOne(a => a.Address, add=>  
  6.   {  
  7.     add.Property(p => p.Street).HasColumnName(nameof(Address.Street));  
  8.     add.Property(p => p.City).HasColumnName(nameof(Address.City));  
  9.     add.Property(p => p.State).HasColumnName(nameof(Address.State));  
  10.   });  
  11.   modelBuilder.Entity<Order>().OwnsOne(a => a.Address);  
  12. }  
After applying the previous code, you can see the difference between the two generated tables,
 
 
One little thing to mention, is that you can exclude a property form being mapped by using the Ignore method,
  1. add.Ignore(p => p.State);  
And if you do not like the Fluent API approach, you can simply decorate the Address class with OwnedAttribute attribute, however, this will not allow you to customize field names and other properties.
  1. [Owned]  
  2. class Address  
  3. {  
  4.   public string Street { getset; }  
  5.   public string City { getset; }  
  6.   public string State { getset; }  
  7. }  
Another thing to mention is that you can create a table for the owned entity using the ToTable method:
  1. modelBuilder.Entity<Customer>().OwnsOne(a => a.Address, add=>  
  2. {  
  3.   add.Property(p => p.Street).HasColumnName(nameof(Address.Street));  
  4.   add.Property(p => p.City).HasColumnName(nameof(Address.City));  
  5.   add.Property(p => p.State).HasColumnName(nameof(Address.State));  
  6.   
  7.   add.ToTable("CustomerAddress");  
  8. });  
And that would create a one-to-one relationship between Customer and the newly generated, CustomerAddress,
 
 
The final thing here is that you can create a one-to-many relationship by using OwnsMany method,
  1. protected override void OnModelCreating(ModelBuilder modelBuilder)  
  2. {  
  3.   base.OnModelCreating(modelBuilder);  
  4.   
  5.   modelBuilder.Entity<Customer>().OwnsMany(a => a.Address);  
  6.   modelBuilder.Entity<Order>().OwnsOne(a => a.Address);  
  7. }  
You need also to update the Customer entity to match the one-to-many relationship:
  1. class Customer  
  2. {  
  3.   [Key]  
  4.   public long CustomerID { getset; }  
  5.   public string Name { getset; }  
  6.   
  7.   public List<Address> Address { getset; }  
  8. }  
The result is as follows,