gpt4 book ai didi

c# - EntityFramework ComplexType 引用 EntityType

转载 作者:太空宇宙 更新时间:2023-11-03 10:29:53 26 4
gpt4 key购买 nike

我对 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; }
}

数据库上下文:

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 的导航属性的情况下,实现这种关系是否有任何注意事项?

谢谢

最佳答案

ComplexType 不能包含 EntityType。只是反过来。

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com