简体   繁体   中英

EF Core Code First Migration requires a primary key for a complex type

I have a data class which contains a subclass. They are used in a DbContext like this:

public class Foo
{
    [Key]
    public int Key { get; set; }

    public SubFoo SubFoo { get; set; }
}

[ComplexType]
public class SubFoo
{
    public double Value1 { get; set; }
    public double Value2 { get; set; }
}

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options) {
    }

    public DbSet<Foo> Foos { get; set; }

}

When trying to create a code-first migration, I get an error

The entity type 'SubFoo' requires a primary key to be defined

Since my class SubFoo is decorated with the attribute ComplexType and that the MSDN documentation states precisely that complex types do not have keys , I am a bit confused.

As mentioned here , I tried to replace the ComplexType annotation with an Owned attribute but it didn't solve the problem.

Am I misunderstanding how to make use of complex types?

Bottom line, after a long investigation, ComplexType are not yet supported in EF Core 2.2 when doing a Code First Migration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM