English 中文(简体)
Entity Framework Tutorial

Entity Framework Resources

Selected Reading

Entity F - Nested Entity Types
  • 时间:2024-11-03

Entity Framework - Nested Entity Types


Previous Page Next Page  

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.

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

pubpc class Person {

   pubpc Person(string fatherName, DateTime birthDate) {
      FatherName = fatherName;
      BirthDate = birthDate;
   }
	
   pubpc string FatherName { get; set; }
   pubpc 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 cpck 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 earper 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.

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

   pubpc class Person {

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

      pubpc string FatherName { get; set; }
      pubpc 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