Working With Complex Type in Entity Framework

Introduction

 
Today, we'll discuss Complex Types in the Entity Framework. As you know, the application data is stored in a table of the database represented in a row-column structure. So, in various cases the row-column format can be directly mapped to the entities, at times you may need to reorganize them.
 
For an example, we have the Student table and in that the Contact and Email represent the contact information of the Student. The entity that represents this table might not be as "flat" as a table. As an example, you can also use a new property named ContactInfo that is of class type and has properties like Mobile and Email. You can do this with the Entity Framework.
 

Working with Complex Types in Entity Framework

 
So, let's now proceed and suppose we have a Student Entity Model as shown below:
 
Student Entity in Entity Framework
 
The Student entity has different columns in the table and from them there are two columns representing the Contact Information named Contact and Email. We can access these properties with the following code:
  1. CollegeEntities CollegeDb = new CollegeEntities();  
  2.    
  3. var DbQuery = from S in CollegeDb.Students  
  4.               where S.StudentID == 1  
  5.               select S;  
  6.    
  7. Student ObjStudent = DbQuery.SingleOrDefault();  
  8.    
  9. Int64 Student_Contact = ObjStudent.Contact;  
  10. string Student_Email = ObjStudent.EmailID; 
Now, suppose we wish to isolate the contact information into a different class. We can do it by the following techniques:
  • Refactor the existing property into a new class
  • Map an existing class with a property
The techniques defined above needs to have a class property of complex type or we can say a property that is of a class type. Let's see how this is to be done with the following sections.
 

Making a Property to Complex Type by Refactor

 
This is the simple way to do this. Use the following procedure.
 
Step 1: Select both properties and just right-click to move them to a New Complex Type.
 
Moving Property to New Complex Type
 
Step 2: After refactoring, both are shifted to the new Complex type. Change the name to StudentContact.
 
Complex Property in Model Browser
 
Step 3: Change the name of the newly created property.
 
Changing Name of Property
 
Step 4: Change the type of Complex property.
 
Setting Complex type of Property
 
Step 5: Now you can access the new property in you code as shown below:
 
Accessing Property by ComplexType
 

Mapping Existing Complex type with Property

 
We saw in the previous section that a new complex type was created as a result of the refactoring process. We can map an existing complex type with a property also. Use the following procedure.
 
Step 1: In the Model browser, right-click on Complex Types to add a new Complex Type.
 
Adding New Complex Type
 
Step 2: Enter the name as StudentContactInfo of that complex type and then right-click to add a new Scalar Property.
 
Adding Scalar Property in Model Browser
 
Step 3: Select the appropriate type for the scalar property as shown below:
 
Creating Property in Model Browser
 
So far the new complex type is not mapped with the Student. We'll now map it.
 
Step 4: Change the type of property to StudentContactInfo as shown below:
 
Changing Type of Complex Property
 
Step 5: Now, right-click on the Student model to open Table Mapping.
 
Table Mapping of Entity
 
Step 6: Now change it to the new complex type as shown below:
 
Creating Table Mapping
 
Step 7: Now the newly mapped property will work like this:
 
Accessing Property in Code
 
That's it.
 

Summary

 
We created a Complex Type property using various techniques using Entity Framework. Happy coding and thanks for reading.