What are complex types in context with the Entity Framework

Now I’m learning a lot about the Entity platform from the Pluralsight video, so excuse my question, which might look like a newbie, but I can’t understand what complex types are or why I need them.

I know that I need to map them through Annotations or Fluent Api something like this:

modelBuilder.ComplexType<blubb>(); 

Maybe someone can develop complex types for me?

+5
source share
3 answers

Suppose you have an entity for courses in a class, this object has the scalar properties Location, Days, and Time, but you will find that you want to abstract so that other objects can use the same model. Thus, you can create a complex type that contains days, location and time to give it a name: ComplexType1. Now other objects can use this type, rather than individual scalar properties, simply by declaring ComplexType1 in the model definition.

+4
source

Complex types repeat structural patterns in your database. You must configure them because there is no way to output it.

An example is two tables in which both columns are associated with an address:

Company

 CompanyName AddressLine1 AddressLine2 Postcode 

Account manager

 Name TelephoneNumber SuperiorName AddressLine1 AddressLine2 Postcode 

This is clearly not a normalized database design, but such situations do occur. You can abstract the model for an address into a complex type, and then specify that both Company and AccountManager have this complex type, and do not preserve the mapping of the mapping columns (although separate in the database) for each table with the 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 is not so heavy, and shows the advantage of comparing two addresses on the same model: http://visualstudiomagazine.com/articles/2014/04/01/making-complex-types-useful.aspx

+5
source

Complex types are types that are not mapped to a table similar to objects; instead, they are mapped to one or more fields.

Next complex type

 public class Descriptor { public string Name {get;set;} public string Description {get;set;} } 

And the essence

 public class MyEntity { public Descriptor { get;set;} } 

This will be displayed in the table with the Name and Description fields. This is a useful way to have the type encapsulate a common set of fields / properties that may be required for multiple objects.

+1
source

Source: https://habr.com/ru/post/1208671/


All Articles