ASP.NET Identity UserID 主键是自定义的外键和主键 table

ASP.NET Identity UserID primary key to be foreign key and primary key in your custom table

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

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

EntityType 'CustomerProfile' has no key defined. Define the key for this 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; }

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

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

public ApplicationUser User { get; set; }

现在可以正常使用了!