首页 > 解决方案 > ASP.NET Identity UserID 主键是自定义表中的外键和主键

问题描述

我正在使用 AspNetIdentity 表,并且我有客户资料的自定义表。我希望表的键与Customer Profile表相同AspnetUser。这可能吗?

我希望作为表中的列的 strUserID 作为AspNetUser表的主键和外键Customer Profile。但我得到一个错误

EntityType 'CustomerProfile' 没有定义键。定义此 EntityType 的键。

代码:

    [Column("strCustomerAddress")]
    [Required(ErrorMessage = "Please enter customer's address.")]
    public string Address { get; set; }

    [Column("strCustomerName")]
    [Required(ErrorMessage = "Please enter customer's name.")]
    public string Name { get; set; }

    [Key]
    [ForeignKey("strUserID")]
    public ApplicationUser User { get; set; }
    public string strUserID { get; set; }

标签: c#asp.netentity-frameworkasp.net-identity

解决方案


问题是您的[Key]属性在导航属性上而不是ForeignKey. 所以写你strUserID的如下:

[Key]
[ForeignKey("User")]
public string strUserID { get; set; }

public ApplicationUser User { get; set; }

现在它可以正常工作了!


推荐阅读