Complex Types

Description Article

Complex Types are objects defined by a composition of scalar properties and with no identity.


What It Is

In the context of C# implementation, there is no difference between this composition and other weaker styles of association.

In the context of ORM this composed class is a Complex Type if it has no individual identity.

In terms of database everything is going to be saved into one single table.


As a limitation of Entity Framework in general, complex types are always considered required.

When saving a new entity instante on the database, the property considered as Complex Type must be intialized, otherwise a “Null value for non-nullable member. Member: ‘XXX‘” is returned.

Entity Framework also returns an initialized complex type when the owning entity is retrieved from the database.

Code First Details: How Are Discovered 

Code First has a concept of Complex Type Discovery that works based on a set of conventions, namelly:

  1. A primary key cannot be inferred;
  2. No primary key is registered through Data Annotations or the fluent API;
  3. The class does not have properties that reference entity types (i.e. all the properties must be scalar types); and
  4. The class type is not referenced from a collection property on another class.

How Are Named 

The database columns are named using the pattern {COMPLEXTYPENAME}_{PROPERTYNAME}.