Using [ComplexType] in Entity Framework Core

Support for complex types is currently on the backlog https://github.com/aspnet/EntityFramework/issues/246


Diego Vega announced Owned Entities and Table Splitting, which is supposed to be a different approach and an alternative to complex types.

Can't share my personal impressions because I haven't checked this personally, but Julie Lerman, seems to have been satisfied...


As an update based on one of your comments above, you now use the OwnsOne syntax for this using the Fluent API in your DbContext's OnModelCreating function.

[ComplexType]
public class Money
{
    public double? Amount { get; set; }
}

public class Rate
{
    [Key]
    public long Id { get; set; }

    public Money Price { get; set; }
}

public MyDbContext : DbContext
{
     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
         modelBuilder.Entity<Rate>(entity =>
         {
             entity.OwnsOne(e => e.Currency);
         });
     }
}

I'm not actually sure if it makes use of the ComplexTypeAttribute or not. But when I generated my migration via Add-Migration, it generated as expected for the old ComplexType documentation this way (i.e. table named Rate has column Price_Amount).