1

场景似乎微不足道,我真的很困惑我做错了什么。

所以,我有一个客户端类

 public class Client
{
    [Key]
    public int ClientID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual Account Account { get; set; }

 }

员工类

public class Employee
{
    [Key]
    public int EmployeeID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public virtual Account Account { get; set; }
}

和一个 Account 类

public class Account
{
    [Key]
    public int AccountID { get; set; }
    public string Login { get; set; }
    public string Password { get; set; }

    public virtual Employee Employee { get; set; }
    public virtual Client Client { get; set; }
}

客户和员工都可能有或没有帐户(在线访问是可选的)。由于数据库与 EF 命名约定不兼容,我必须提出 Fluent API 显式映射。

Client 和 Employee 表都有我试图用来建立关系的“AccountID”列。

        modelBuilder.Entity<Client>()
            .HasOptional(e => e.Account)
            .WithRequired(a => a.Client)
            .Map(m => m.MapKey("AccountID"));

        modelBuilder.Entity<Employee>()
            .HasOptional(e => e.Account)
            .WithRequired(a => a.Employee)
            .Map(m => m.MapKey("AccountID"));

但我明白了

Schema specified is not valid. Errors: 
(15,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined.
(16,6) : error 0019: Each property name in a type must be unique. Property name 'AccountID' was already defined.

那么,除了修改表/实体结构之外,有没有办法解决这个问题?

4

1 回答 1

0

事实证明,在这种情况下您不需要 Fluent API,您需要的是 DataAnnotate 具有 InverseProperty 属性的实体中的属性

[InverseProperty("AccountID")]

Ladislav Mrnka 在Entity Framework 4.1 InverseProperty Attribute question中有一个很好的答案

但是,如果有人知道如何使用流利的答案正确地做到这一点,我们将不胜感激

于 2012-10-16T16:22:53.980 回答