- Entity F - Nested Entity Types
- Entity F - Multiple DbContext
- Entity F - Code First Migration
- Entity Framework - Seed Database
- Entity Framework - Fluent API
- Entity Framework - Data Annotations
- Entity Framework - First Example
- Entity F - Code First Approach
- Entity Framework - Colored Entities
- Entity Framework - Track Changes
- Entity Framework - Validation
- Entity Framework - Explicit Loading
- Entity Framework - Lazy Loading
- Entity Framework - Eager Loading
- Entity Framework - Migration
- Entity Framework - Inheritance
- Entity Framework - Spatial Data Type
- Entity F - Command Interception
- Entity F - Command Logging
- Entity F - Projection Queries
- Entity Framework - Persistence
- Entity F - Asynchronous Query
- Entity Framework - Enum Support
- Entity Framework - Native SQL
- Entity F - Table-Valued Function
- Entity F - Disconnected Entities
- Entity F - Stored Procedures
- Entity Framework - Index
- Entity Framework - Views
- Entity Framework - Transaction
- Entity Framework - Concurrency
- Entity F - Database Operations
- Entity Framework - DEV Approaches
- Entity F - Database First Approach
- Entity F - Model First Approach
- Entity F - Code First Approach
- Entity Framework - Lifecycle
- Entity Framework - Relationships
- Entity Framework - Types
- Entity Framework - DbContext
- Entity Framework - Data Model
- Entity Framework - Database Setup
- Entity F - Environment Setup
- Entity Framework - Architecture
- Entity Framework - Overview
- Entity Framework - Home
Entity Framework Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Entity Framework - Nested Entity Types
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)
Now you will see that Identity property is defined in Student class.
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.
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