What is a complex type in entity framework and when to use it?

asp.net c# entity-framework


I attempted to read the complex types section of the MSDN article. However, it doesn't specify when to employ it. Additionally, there isn't a thorough explanation of complex types and their use on the internet.

6/14/2016 7:56:24 AM

Accepted Answer

You've linked to a lengthy explanation in the MSDN page, so basically you just need an easy explanation:

Instead of having its own table, a complex type maps its properties to columns on an already-existing table (the one for the entity that contains it) in its own object for C# (which would need a key, etc.).

Assume that you want the following table in the database:

Id (bigint)
Name (varchar)
Street (varchar)
Region (varchar)
Country (varchar)

However, I need this structure in C# entities:

class Order
   long Id;
   string Name;

   struct Address
     string Street;
     string Region;
     string Country;

then thereAddress would be a complex type; it wouldn't exist independently (thereAddresses It would only exist as a set of columns on the table (in the database).Orders table.

The benefit of having complex types, as mentioned by @HenkHolterman in the comments, is having a single C# entity that can be used as a value for other contained entities (in my example, you could have an array of complex types).Address in aSupplier for instance, however it will only be mapped as a collection of columns in theSuppliers table). Working with the values in the complicated type is made simple by this.

The drawback is just that: if the same complex type value appears more than once in the database, you may have to repeat it.Address (or any other type you choose to choose) can be shared by other entities.

It is up to you and your design whether you decide to work with complex types or independent entities.

6/14/2016 8:13:26 AM

Popular Answer


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow