Entity Framework - Nested Entity Types


Advertisements


Prior to Entity Framework 6, Entity Framework didn't recognize entities or complex types that were nested within other entities or complex types. When Entity Framework generated the model, the nested types just disappeared.

Let’s take a look at a simple example in which we have our basic model with three entities Student, Course and Enrollment.

  • Let’s add a property Identity, which is a Person type. Person is another entity, contains BirthDate and FatherName properties.

  • In Entity Framework terms, because it has no identity and is part of an entity, it's an Entity Framework complex type, and we've actually had support for complex types since the first version of Entity Framework.

  • The Person type isn't nested as shown in the following code.

public class Student {
   public int ID { get; set; }
   public string LastName { get; set; }
   public string FirstMidName { get; set; }
   public DateTime EnrollmentDate { get; set; }
   public Person Identity { get; set; }
	
   public virtual ICollection<Enrollment> Enrollments { get; set; }
}

public class Person {

   public Person(string fatherName, DateTime birthDate) {
      FatherName = fatherName;
      BirthDate = birthDate;
   }
	
   public string FatherName { get; set; }
   public DateTime BirthDate { get; set; }
}

Entity Framework will know how to persist Person types when it is used in previous versions as well.

By using Entity Framework Power Tool we will see how Entity Framework interprets the model. Right click on Program.cs file and select Entity Framework → View Entity Data Model (Read only)

Framework Power Tool

Now you will see that Identity property is defined in Student class.

Identity Property

If this Person class won't be used by any other entity, then we can nest it inside the Student class, but this earlier version of Entity Framework doesn't acknowledge nested types.

In older version, you generate the model again, not only is the type not recognized, but because it's not there, the property isn't there either, so Entity Framework won't persist the Person type at all.

public class Student {
   public int ID { get; set; }
   public string LastName { get; set; }
   public string FirstMidName { get; set; }
	
   public DateTime EnrollmentDate { get; set; }
   public Person Identity { get; set; }
	
   public virtual ICollection<Enrollment> Enrollments { get; set; }

   public class Person {

      public Person(string fatherName, DateTime birthDate) {
         FatherName = fatherName;
         BirthDate = birthDate;
      }

      public string FatherName { get; set; }
      public DateTime BirthDate { get; set; }
   }
}

With Entity Framework 6, nested entities and complex types are recognized. In the above code, you can see that Person is nested within the Student class.

When you use the Entity Framework Power Tool to show how Entity Framework interprets the model this time, there's true Identity property, and Person complex type. So Entity Framework will persist that data.

Nested Entity Type

Now you can see that Identity is a nested entity type, which was not supported before Entity Framework 6.

We recommend that you execute the above example in a step-by-step manner for better understanding.



Advertisements
E-Books Store