【问题标题】:Mapping a foreign key with a different name映射具有不同名称的外键
【发布时间】:2013-03-24 20:35:44
【问题描述】:

我有以下型号:

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

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

【问题讨论】:

    标签: c# entity-framework foreign-keys


    【解决方案1】:

    使用ForeignKey 属性。

    public class Task
    {
        public int Id { get; set; }
        public int CreatorId { get; set; }
    
        [ForeignKey("CreatorId")]
        public virtual Useraccount Useraccount { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多