简体   繁体   English

EF Core 代码优先迁移需要复杂类型的主键

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

I have a data class which contains a subclass.我有一个包含子类的数据 class 。 They are used in a DbContext like this:它们在DbContext中使用,如下所示:

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实体类型“SubFoo”需要定义主键

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.由于我的 class SubFoo装饰有ComplexType属性,并且MSDN 文档明确指出复杂类型没有键,我有点困惑。

As mentioned here , I tried to replace the ComplexType annotation with an Owned attribute but it didn't solve the problem.如此处所述,我尝试将ComplexType注释替换为Owned属性,但并没有解决问题。

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.最重要的是,经过长期调查,EF Core 2.2 在执行代码优先迁移时尚不支持 ComplexType。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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