Entity Framework Complex Entity Mapping

Entity Framework can have in its EDMX file complex entities. The “normal” property of an entity is a “Scalar entity”.
Scalar entity is a property of the CLR type.

Scalar Types

Scalar types

The complex entity is not a CLR type. In fact, it’s a group of scalar entities. From the point of view of the database, nothing change either if it’s a complex or scalar property. But, from the point of view of the Object Context, the entity will have an object with a group of scalar property.

How to add Complex entity?

In the AdventureWorks database once the model is generated from the database you get tables, views and stored procedure. For this exercise, lets take the generated entity “Department”

DepartmentEntity

Department Entity


DepartmentID, Name, GroupName and ModifiedDate are all scalar entities. If we want to add a Complex Entity, we need to add a complex type that will be added to the entity. To add a complexe type, right-click the Visual Designer and select Add>Complex Type.

Adding a complex type

Context Menu to add a Complex Type

One the complexe type is created, this one will be added in the Model Browser under the Entity Model (.edmx file), under the folder “Complex types”. From there, it’s possible to right click the complex entity to add scalar entity or even other complex entity.

Complex type with properties

Once the complex type is created, it’s possible to add it as a complex property. Right clicking on the entity and selecting add and complex property. The last step is to select the complex property and to go to the property window to select the type of the created type.

From here, it’s possible to access the complex property and will be even in the Intellisense.

var db = new AdventureWorks_DataEntities();
var aSingleDepartment = db.DepartmentList.Where(dept => dept.DepartmentID == 1).FirstOrDefault<Department>();
int theIntegerPropertyOfMyComplexPropertyOfDepartment = 
                  aSingleDepartment.MyComplexProperty.MyFirstIntProperty;           

The only thing that remain to do before compiling is a last mapping for the complex entity. This is needed because you will receive an error message if not.

Error 3004: Problem in mapping fragments starting at line 5050: No mapping specified for properties Department.
MyComplexProperty in Set DepartmentList.
An Entity with Key (PK) will not round-trip when: Entity is type [AdventureWorks_DataModel.Department]

This last step is concerning the mapping with the database.

Database

What about the database? Well, once the entity is modified in the entity model, it requires to be synchronized with the database. This is done by right clicking the Visual Designer and selecting “Generate database model from model”. This will override the DDL file (Data Definition Language), the SSDL (Store schema definition language) and the MSL (Mapping Specification Language) files.

If you like my article, think to buy my annual book, professionally edited by a proofreader. directly from me or on Amazon. I also wrote a TypeScript book called Holistic TypeScript

2 thoughts on “Entity Framework Complex Entity Mapping

  1. “The only thing that remain to do before compiling is a last mapping for the complex entity. This is needed because you will receive an error message if not.”

    And finding out where to do this is our homework?

    • Hi Micheal,

      I may miss understand your comment but I found your tone a little but upset and I am wondering why? Would have you prefer that I never wrote this article? My goal was to give some information concerning Entity Framework with complex entity, for free, for anybody. You can, and I strongly suggest your, that you write the answer of your question in a new comment. This might help somebody in your situation later. This will be constructive.

      For me, this is a old post and I suggest you to jump to Code First with Entity Framework instead of doing it with EDMX file.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.