EF Core ComplexType Attribute

Starting from EF Core 8, it's now possible to set a class or a structure as a Complex Type. The following example specifies that the Address class should be considered as a complex type:

language-csharp
|
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public class Order
{
    public int OrderId { get; set; }
    public DateTime OrderDate { get; set; }
    [Required]
    public Address ShippingAddress { get; set; }
    [Required]
    public Address BillingAddress { get; set; }
    //... other order-related properties
}

[ComplexType]
public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Country { get; set; }
    public string PostalCode { get; set; }
}

One particularity of the complex type is that you also have to mark complex property as Required

Fluent API

The Fluent API equivalent to the ComplexType attribute is the ComplexProperty method.


Date Modified: 2023-10-24
Author:

Edit this page in GitHub

Got any EF Core Question?