Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are Complex Types in Context with Entity Framework

Right now I am learning a lot about the Entity Framework from Videos on Pluralsight, so excuse my Question which might look newbish but I can not understand what Complex Types are or why I would need them.

I do know that I have to map them via Annotations or Fluent Api something like this:

modelBuilder.ComplexType<blubb>();

Maybe someone could elaborate the need of Complex Types for me?

like image 336
CodeFanatic Avatar asked Dec 09 '14 15:12

CodeFanatic


2 Answers

Assume you have an entity for Courses in a class, that entity has scalar properties of Location, Days and Time, but you find you want to abstract that so that other entities can use the same model. So you can create a complex type that contains Days, Location and Time give it a name: ComplexType1. Now other entities can use this type rather than individual scalar properties just by declaring ComplexType1 in the model definition.

like image 193
JWP Avatar answered Sep 27 '22 21:09

JWP


Complex Types are repeating structural patterns in your database. You have to custom map them because there is no way for it to be inferred.

An example would be two tables that both have address related columns:

Company

CompanyName
AddressLine1
AddressLine2
Postcode

Account Manager

Name
TelephoneNumber
SuperiorName
AddressLine1
AddressLine2
Postcode

This is obviously not well-normalized database design but such situations do occur. You can abstract the model for address into a complex type, then specify that both Company and AccountManager have that complex type rather than keep mapping the matching (albeit separate in the database) columns for each table that has address columns.

Here's an in depth article on Complex Types: http://msdn.microsoft.com/en-gb/data/jj680147.aspx

And here is one that isn't quite so heavy, and shows the benefit of mapping two addresses on one model, things like that: http://visualstudiomagazine.com/articles/2014/04/01/making-complex-types-useful.aspx

like image 41
Steve Lillis Avatar answered Sep 27 '22 21:09

Steve Lillis