我是EntityFramework的Code First方法的新手,在尝试创建引用实体类型的可重用复杂类型时遇到以下错误。

楷模:

 class Bank
{
    public int Code { get; set; }
    public string Name { get; set; }
}
 class BankAccount
{
    public Bank Bank { get; set; }
    public int BankId { get; set; }
    public int Agency { get; set; }
    public int Account { get; set; }

}
 class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime BirthDate { get; set; }
    public BankAccount BankAccount { get; set; }
}


DbContext:

class DemoContext : DbContext
{

    public DbSet<Bank> Banks { get; set; }
    public DbSet<Person> People { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

        modelBuilder.Entity<Bank>().HasKey(b => b.Code);
        modelBuilder.ComplexType<BankAccount>();

    }

}


当我尝试添加迁移时,出现以下错误:

One or more validation errors were detected during model generation:

ComplexTypeProblem.EF.Bank: Name: Each type name in a schema must be unique. Type name 'Bank' is already defined.
ComplexTypeProblem.EF.Bank: : EntityType 'Bank' has no key defined. Define the key for this EntityType.
Banks: EntityType: EntitySet 'Banks' is based on type 'Bank' that has no keys defined.


在ComplexType具有到EntityType的导航属性的情况下,是否有任何警告要实现这种关系?

谢谢

最佳答案

您不能有一个包含EntityType的ComplexType。只是相反。

ComplexTypes只是实体的属性,它们应该像普通字段一样在您的代码上工作。

关于c# - EntityFramework ComplexType引用EntityType,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30577169/

10-09 10:09