Home » Ado.Net » Entity Framework » Understand basic navigation with Entity Framework Code First (4.3) with 1 to 0..1 relationship

Understand basic navigation with Entity Framework Code First (4.3) with 1 to 0..1 relationship

Most of the work is done in your DbContext when we talk about configuration of your entities. This is also true for navigation properties that can be set via the fluent interface of Entity Framework.

The first step is to override the method OnModelCreating.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
   //...
}

From there you can work in two different ways, the first one is to create class for each entity’s configuration. The second one is to work directly into the OnModelCreation method. I prefer the first one because it’s cleaner with bigger project.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    modelBuilder.Configurations.Add(new MyEntityConfiguration());
}
//...
public class MyEntityConfiguration: EntityTypeConfiguration<MyEntity> 
{
    public MyEntityConfiguration()
    {
        //Configurations here
    }
}

As you can see, the type inherited by the configuration class is EntityTypeConfiguration but could be ComplexTypeConfiguration for complex type instead of entity type.

To make it shorter, for the purpose of this blog post, I won’t use the classes approach but in real life, I would suggest your to use it.

HasRequired and HasOptional

This two keywords work in conjunction of the use of virtual object inside your classes. They will specify the type of relationship between entities.

HasRequired is implicit if you declare a virtual property for your class. For example, the code below display 2 classes and both of them refer to each of them. This is a 1 to 1 implicit relationship.

public class User
{
    public int Id { get; set; }
    public string Username { get; set; }
    public virtual Profile Profile { get; set; }
}

public class Profile
{
    public int Id { get; set; }
    public string PostalCode { get; set; }
    public virtual User User { get; set; }
}

No configuration is required, nevertheless, you could have wrote in the DbContext some code that explicit the navigation type.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
    modelBuilder.Entity<User>().HasRequired(t => t.Profile);
}

This scenario is good for the case that a “User” require to have a “Profile” but “Profile” class can be created without being linked to a “User”. If you want to specify if it’s the class “Profile” or user “User” that will contain the relationship than you need to use the clause WithRequiredPrincipal or WithOptionalPrincipal.

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{
modelBuilder.Entity<User>()
   .HasRequired(t => t.Profile)
   .WithRequiredPrincipal(t => t.User);
}

The first code that were using HasRequired only was in fact using implicitly “WithOptional(t=>t.User)”. The User would have the relation inside the table and not profile. The second code also specify with WithRequiredPrincipal that the User will hold the foreign key.

Has or With

You have HasRequired, HasOptional, HasMany and HasForeignKey. In the same time you have WithRequired, WithOptional, WithMany. What is the main difference between those method with Has and With. The first one, the Has, represent the navigation property. The second one, the With represent the reverse property configuration. Let say you have class A and class B. If you configure the entity A every call with the keywork Has will be on the class A property (you can see this within the lambda expression that will be of type A with the return of type B. However, if you use property with With in prefix, you will configure B to A relationship. For example :

modelBuilder.Entity<A>().HasRequired(a => a.B).WithOptional(b => b.A);

To clarify even more the Has and With, here is a more complex scenario that involve many classes.

Lets focus on the Car class and the Customer class. In this scenario, a car can have or not a owner (Customer). The customer can have, or hot a current car (Car). This is a circular reference because the current car and the current owner are always the same. They are 3 ways to do it with Entity Framework Code First using Fluent API. The first one is to define the Entity Type Configuration on the Car. The second one is to define the relationship on the Customer Entity Type Configuration. The third possibility is to defined both side. Of course, the last one will require you to have something cohesive between both configuration but has the advantage to be very explicit.

        public class CarConfiguration : EntityTypeConfiguration<Car>
        {
            public CarConfiguration()
            {
                HasOptional(x => x.CurrentOwner).WithOptionalPrincipal(t => t.CurrentCar); //User contain the Customer FK
            }
        }

        public class CustomerConfiguration : EntityTypeConfiguration<Customer>
        {
            public CustomerConfiguration()
            {
                HasKey(x => x.Id);
                HasOptional(x => x.CurrentCar).WithOptionalDependent(t => t.CurrentOwner);//Defined inside the CarConfiguration (up-side-down), may not be required
            }
        }

In the first scenario, we would have only the HasOptional statement on the CarConfiguration class without having anything in the CustomerConfiguration. It’s the reverse for the second way. The last one is exactly the whole code you see above, with both navigation defined.

In the case you think you can only setup the HasOptional on both without setuping up the WithOptionalPrincipal or WithOptionalDependent you are wrong. This will lead to an error :

Unable to determine the principal end of an association between the types ‘EFCodeFirst.Models.Car’ and ‘EFCodeFirst.Models.Customer’. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

How come? Because it’s impossible for Entity Framework to know who’s between the Customer or the Car contain the reference of the object. This is because if one of the object require to have the reference and the other one not, Entity Framework will create the table with the reference on the table with the required FK required. This is why having the code below won’t work and raise the error described.

        public class CarConfiguration : EntityTypeConfiguration<Car>
        {
            public CarConfiguration()
            {
                HasKey(x => x.Id);
                HasOptional(x => x.CurrentOwner); //Error!!!
            }
        }
        public class CustomerConfiguration : EntityTypeConfiguration<Customer>
        {
            public CustomerConfiguration()
            {
                HasKey(x => x.Id);
                HasOptional(x => x.CurrentCar);// Error!!!
            }
        }

The only time you can write HasOptional or HasRequired without defining the other side of the navigation is when you have the class having the reference to the other object and that the other one doesn’t. If we want to do it with our class Car and Customer we just need to remove from Customer the Car property like the following code.

   public class Car:BaseEntity
    {
        public Customer CurrentOwner { get; set; }      //Relationship : One to (zero or one)
    }
    public class Customer:BaseEntity
    {
   
        //public Car CurrentCar { get; set; }                     //Relationship: One to (zero or one)
        public License License { get; set; }                    //Relationship: One to (zero or one)
    }

        public class CarConfiguration : EntityTypeConfiguration<Car>
        {
            public CarConfiguration()
            {
                HasKey(x => x.Id);
  
                HasOptional(x => x.CurrentOwner);
            }
        }
        public class CustomerConfiguration : EntityTypeConfiguration<Customer>
        {
            public CustomerConfiguration()
            {
                //Nothing
            }
        }

This will work.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

3 Responses so far.

  1. […] configuring Entity Framework Code First for 1 to 1 or 1 to 0 relation, you need to go in your DbContext and to configure your […]

  2. Steven says:

    Really, no comment on this post ?
    Thank you very much, it was very helpful !

  3. propediotika says:

    Thank you for explanation.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.