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:

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:
- CollegeEntities CollegeDb = new CollegeEntities();
- var DbQuery = from S in CollegeDb.Students
- where S.StudentID == 1
- select S;
- Student ObjStudent = DbQuery.SingleOrDefault();
- Int64 Student_Contact = ObjStudent.Contact;
- 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.

Step 2: After refactoring, both are shifted to the new Complex type. Change the name to StudentContact.












Nilesh DeolikarPosted May 27, 2020, 9:45 AM
How to use and oprator
chatay gurPosted Dec 18, 2018, 1:08 PM
Good article