简体   繁体   English

映射具有不同名称的外键

[英]Mapping a foreign key with a different name

I have the following model: 我有以下模型:

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

The useraccountId is my foreign key showing to my useraccount entity/model. useraccountId是显示给我的useraccount实体/模型的我的外键。 The entity framework can map this foreign key to the virtual property because it removes the "Id", so "useraccountId" becomes "useraccount" -> mapping to "Useraccount". 实体框架可以将此外键映射到虚拟属性,因为它删除了“ Id”,因此“ useraccountId”变为“ useraccount”->映射到“ Useraccount”。 What if I want to rename the foreign key "useraccountId" to "creatorId"? 如果我想将外键“ useraccountId”重命名为“ creatorId”怎么办? How can I now tell the Entity framework to map this foreign key to the virtual Useraccount property? 现在如何告诉Entity框架将此外键映射到虚拟Useraccount属性?

Use the ForeignKey attribute. 使用ForeignKey属性。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM