扩展 Asp 网络核心身份关系
Extend Asp Net Core Identity Relation
在 Asp net core (3.1) Identity 中,我想在用户和 TourOperators 之间添加多对多关系。
(概念是许多用户可以跟随许多旅行社)。
我有旅行社class:
public class TourOperator
{
public int Id { get; set; }
public ICollection<Follow> Follows { get; set; }
}
我已经扩展了 UserIdentity class:
public class ApplicationUser: IdentityUser
{
public ICollection<Follow> Follow { get; set; }
}
我有关注class:
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 table中我有4个字段而不是3个?
我有以下领域:
我认为 ApplicationUserId 不存在
Entity Framework 无法 link 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");
在 Asp net core (3.1) Identity 中,我想在用户和 TourOperators 之间添加多对多关系。 (概念是许多用户可以跟随许多旅行社)。
我有旅行社class:
public class TourOperator
{
public int Id { get; set; }
public ICollection<Follow> Follows { get; set; }
}
我已经扩展了 UserIdentity class:
public class ApplicationUser: IdentityUser
{
public ICollection<Follow> Follow { get; set; }
}
我有关注class:
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 table中我有4个字段而不是3个?
我有以下领域:
我认为 ApplicationUserId 不存在
Entity Framework 无法 link 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");