asp.net-core - 扩展 Asp Net Core 身份关系
问题描述
在 Asp net core (3.1) Identity 中,我想在用户和 TourOperators 之间添加多对多关系。(这个概念是许多用户可以跟随许多旅行社)。
我有旅行社类:
public class TourOperator
{
public int Id { get; set; }
public ICollection<Follow> Follows { get; set; }
}
我扩展了 UserIdentity 类:
public class ApplicationUser: IdentityUser
{
public ICollection<Follow> Follow { get; set; }
}
我有关注类:
public class Follow
{
public long Id { get; set; }
public string UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public int TourOperatorId { get; set; }
public TourOperator TourOperator{ get; set; }
}
执行迁移后,为什么在 Follow 表中我有 4 个字段而不是 3 个?我有以下领域:
我认为 ApplicationUserId 不能存在
解决方案
Entity Framework 没有办法链接UserId
和ApplicationUser
属性。因此,您要么需要遵循约定,因此 EF 可以做出有根据的猜测。最简单的选择是重命名您的string
属性:
public string ApplicationUserId { get; set; }
或者,您可以配置它,例如使用属性:
[ForeignKey("ApplicationUser"]
public string UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
或者在OnModelCreating
方法中,例如:
modelBuilder.Entity<Follow>()
.WithOne(f => f.ApplicationUser)
.HasForeignKey("UserId");
推荐阅读
- ssl - cURL vs Postman,对 HTTPS POST 的不同响应
- reactjs - 参数不更新状态变化,反应原生
- javascript - 切换对象属性VueJS?
- apache-kafka - kafka + 如何将特定主题的特定保留字节设置为永久网络
- python - 将 DSV 转换为 CSV
- nginx - 启用身份验证网关后 phpmyadmin 500 内部服务器错误
- swift - UIlabel 中的文本模糊
- java - 与 Java SE 相比,JDBC 在 Java EE 上的行为是否不同?
- javascript - Object.assign() 没有按原样构造我的对象数组
- javascript - 使用 map 设置对象数组返回相同的值