使用其他名称映射外键

时间:2013-02-26 08:43:12

标签: c# entity-framework foreign-keys

我有以下型号:

public class Task
{
    public int Id { get; set; }
    public int UseraccountId { get; set; }
    public virtual Useraccount Useraccount { get; set; }
}

useraccountId是我的useraccount实体/模型显示的外键。 实体框架可以将此外键映射到虚拟属性,因为它删除了“Id”,因此“useraccountId”变为“useraccount” - >映射到“Useraccount”。 如果我想将外键“useraccountId”重命名为“creatorId”怎么办?我现在如何告诉Entity框架将此外键映射到虚拟Useraccount属性?

1 个答案:

答案 0 :(得分:3)

使用ForeignKey属性。

public class Task
{
    public int Id { get; set; }
    public int CreatorId { get; set; }

    [ForeignKey("CreatorId")]
    public virtual Useraccount Useraccount { get; set; }
}