In this article
ComplexType Attribute
The Data annotations Complex type attribute is used to define the non-scalar properties of an entity. You can read more about from Data Annotations in Entity Framework Tutorial
This attribute is not supported in EF Core
Software Versions used in the tutorial
For EF 6 Visual Studio 2015 Entity framework 6.2.0. .NET 4.5 C# | For EF Core Visual Studio 2017 Entity framework Core 2.1.3. .NET Core 2.1 C# |
What is Complex Type
Consider the entity model class below. Here the ContactInfo is a complex type, which is shared by both Customer and Employee entity. All you need to do is to decorate the class with ComplexType Attribute. The ComplexType Attribute resides in the System.ComponentModel.DataAnnotations.Schema
1 2 3 4 5 6 7 8 9 10 11 12 | public class Customer { public Customer() { Contact = new ContactInfo(); } public int CustomerID { get; set; } public string Name { get; set; } public ContactInfo Contact { get; set; } } |
1 2 3 4 5 6 7 8 9 10 11 12 | public class Employee { public Employee() { EmployeeContact = new ContactInfo(); } public int EmployeeID { get; set; } public string Name { get; set; } public ContactInfo EmployeeContact { get; set; } } |
1 2 3 4 5 6 7 8 | [ComplexType] public class ContactInfo { public string Email { get; set; } public string Phone { get; set; } } |
When you Add the ComplexType Attribute the EF does not generate the table for the class. The above entity models are mapped to the database as shown in the image above. The Column names are created with the name <PropertyName>_<ComplexTypePropertyName>
Complex Type in EF Core
EF Core does not support ComplexType attribute as of now
Leave a Reply