【问题标题】:Make foreign key to ApplicationUser in auxiliary table (EntityFramework Identity)在辅助表中为 ApplicationUser 创建外键(EntityFramework Identity)
【发布时间】:2015-12-22 07:13:13
【问题描述】:

我的 ApplicationUser 模型定义如下:

public class ApplicationUser : IdentityUser
{
    public virtual UserProfileInfo UserProfileInfo { get; set; }
}

UserProfileInfo 是一个包含额外用户数据的辅助表,定义如下:

public class UserProfileInfo
{
    public int Id { get; set; }
    public string SuperiorId { get; set; }
    [ForeignKey("SuperiorId")]
    public virtual ICollection<ApplicationUser> Superior { get; set; }
}

在业务层次结构中,每个用户都有一个或多个优于他们的人。但是,尝试进行迁移会导致以下错误:

类型“Project.Models.UserProfileInfo”的属性“Superior”上的 ForeignKeyAttribute 无效。在依赖类型“Project.Models.ApplicationUser”上找不到外键名称“SuperiorId”。

在我的情况下,如何对 ApplicationUser 进行外键引用?

【问题讨论】:

    标签: asp.net-mvc entity-framework


    【解决方案1】:

    你需要反过来做,ID是导航属性的外键:

    public class UserProfileInfo
    {
       public int Id { get; set; }
       [ForeignKey("Superior")]
       public string SuperiorId { get; set; }
    
       public virtual ICollection<ApplicationUser> Superior { get; set; }
    }
    

    【讨论】:

    • 谢谢我对如何建立外键/导航属性感到困惑。
    猜你喜欢
    • 2016-03-14
    • 2015-01-10
    • 2020-08-16
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多