Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 实体框架,并使用Fluent API将两个实体映射到另一个实体_Asp.net Mvc 4_Entity Framework 5_Fluent Interface - Fatal编程技术网

Asp.net mvc 4 实体框架,并使用Fluent API将两个实体映射到另一个实体

Asp.net mvc 4 实体框架,并使用Fluent API将两个实体映射到另一个实体,asp.net-mvc-4,entity-framework-5,fluent-interface,Asp.net Mvc 4,Entity Framework 5,Fluent Interface,这个场景看起来很琐碎,我真的很困惑我做错了什么 所以,我有一个客户机类 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 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; }
}
还有一个会计类

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 namingconvention不兼容,我必须提出流畅的API显式映射

客户机表和雇员表都有“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"));

那么,除了修改表/实体结构之外,还有其他方法可以解决这个问题吗?

事实证明,在这种情况下,您不需要Fluent API,您需要的是使用InverseProperty属性对实体中的属性进行数据注释

[InverseProperty("AccountID")]
拉迪斯拉夫·姆尔恩卡(Ladislav Mrnka)给出了一个很好的答案

然而,如果有人知道如何正确地回答,我们将不胜感激

[InverseProperty("AccountID")]