实体框架代码第一个映射 - 非外键

时间:2014-03-27 06:52:13

标签: entity-framework ef-code-first

我今天提出了以下方案,在那里我发现配置代码第一次流畅映射有困难

public class Employee
{
    public int Id { get; set; }
    public string ReferralCode { get; set; }
    public string ReferedByCode{ get; set; }

    public virtual ICollection<Employee> ReferedEmployee { get; set; }

    public virtual  Employee ReferedBy { get; set; }
}

每位员工都会被其他员工推荐,就像该员工会有很多推荐员工一样。 推介将基于员工代码 而非ID。

如何为此配置流畅的映射?

1 个答案:

答案 0 :(得分:0)

我认为不可能将导航属性配置为使其外键与相关实体的主键以外的列/属性相关。

您的选择是:

  1. 将主键更改为ReferralCode
  2. 将主键保留为Id并执行以下操作:
    • 添加ReferredById列和use this as the foreign key
    • Enable migrations(如果它们尚未启用)。
    • 添加迁移以添加ReferredById列并对其进行编辑,以便在调用Sql() method时运行SQL脚本,以便根据以下内容更新此新列中的值现有ReferredByCode列。