• Skip to content
  • Skip to primary sidebar
  • Home
  • Angular
  • ASP.NET Core
  • Entity Framework
    • Entity Framework 6
    • Entity Framework Core
  • Crystal Reports
  • C#
  • ASP.NET
  • About Us
    • Privacy Policy
    • Contact Us

TekTutorialsHub

Free Online Tutorials

Data Annotations ComplexType Attribute in EF 6 & EF Core

January 17, 2016 by TekTutorialsHub Leave a Comment

Key Attribute
Concurrencycheck Attribute
 

In this article

  • ComplexType Attribute 
  • What is Complex Type
  • Complex Type in EF Core

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>

Data Annotations Complex Type Attribute in Entity Framework Code First
Data Annotations Complex Type Attribute in Entity Framework Code First

Complex Type in EF Core

EF Core does not support ComplexType attribute as of now

Key Attribute
Concurrencycheck Attribute
 

Filed Under: Entity Framework Tagged With: Data Annotations

Leave a Reply

wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.
wpdiscuz_captcharefresh
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of

Primary Sidebar

Copyright ©2008-2018

About us Contact Privacy Policy

Feb,22,2019 01:31:43 PM

Copyright © 2019 · Magazine Pro on Genesis Framework · WordPress · Log in

wpDiscuz
Our web site uses cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.OkRead more