c# - 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; }
解决方案
问题是您的[Key]
属性在导航属性上而不是ForeignKey
. 所以写你strUserID
的如下:
[Key]
[ForeignKey("User")]
public string strUserID { get; set; }
public ApplicationUser User { get; set; }
现在它可以正常工作了!
推荐阅读
- django - 在 Django/Vue 应用程序中更改公共 index.html 中的 chunk_vendors/app js 文件导入 url
- laravel - 使用一些数据过滤从日期到日期
- javascript - 如何使用 Javascript 随机化 WordPress 中的图像(高级自定义字段)
- f# - 为什么在这种情况下在 F# 中使用引用
- swift - 如何在类内设置计算值的绑定(SwiftUI)
- pyspark - 使用 pyspark 联合 Delta Lake 表
- javascript - 自动滚动到自动完成中的特定选项
- python - 即使我在运行代码后有一个结束条件,我的循环也会无休止地运行
- python - x.reshape([1,28,28,1]) 重塑意义
- api - BigCommerce 通过电子邮件收到的密码重置链接需要通过 Headless App 用于通过 API 调用重置密码吗?