ASP Net Core:与IdentityUser添加多对多关系 [英] ASP Net Core: add many to many relationship with IdentityUser
问题描述
我需要在ASP网络核心中与UserIdentity添加多对多关系(即:一个用户可以有很多书,一本书可以有很多用户所有者)
i need to add a many to many relationship with UserIdentity in asp net core (i.e: a user can have many books and a book can have many user owners)
我有这本书的课程:
public class Book
{
public int Id { get; set; }
}
我扩展了UserIdentity类:
I have extended the UserIdentity class:
public class ApplicationUser : IdentityUser
{
public ICollection<UserBook> UserBooks { get; set; }
}
我已经创建了联接表,但是我不知道如何引用identityuser id
I have created the join table but i don't know how reference the identityuser id
public class UserBook
{
public int UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
}
如何正确创建identityuser表和另一个用户之间的多对多关系表?
How can create correctly a many to many relationship between the identityuser table and another table?
谢谢
推荐答案
在您的您已使用类型为
模型类> ApplicationUser ,但是在您的 int
类型的 UserId
的> UserBook ApplicationUser
模型类中,您继承的是 IdentityUser
,其中主键 Id
默认情况下为 string
类型,这意味着您的 ApplicationUser
' s Id
的类型为 string
。因此, UserBook
表中的 Foreignkey
将存在主键类型不匹配。
In your UserBook
model class you have used UserId
of type int
for ApplicationUser
but in your ApplicationUser
model class you are inheriting IdentityUser
where primary key Id
is of type string
by default, that means your ApplicationUser
's Id
is of type string
. So there will be a primary key type mismatch for Foreignkey
in UserBook
table.
解决方案1::如果没有问题,请保留 ApplicationUser
的主键 Id
类型 string
,然后只需将 UserId
类型更改为 UserBook
模型类如下:
Solution-1: If you have no problem to keep ApplicationUser
's primary key Id
of type string
, then just change the UserId
type to string in UserBook
model class as follows:
public class UserBook
{
public string UserId { get; set; }
public ApplicationUser ApplicationUser { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
}
解决方案2:更改 ApplicationUser
的主键 Id
从默认的 string
类型到 int
,然后在继承 IdentityUser $的同时将键类型指定为
int
c $ c>如下:
Solution-2: If you want to change ApplicationUser
's primary key Id
from default string
type to int
, then specify the key type as int
while you are inheriting IdentityUser
as follows:
public class ApplicationUser : IdentityUser<int>
{
public ICollection<UserBook> UserBooks { get; set; }
}
现在,您必须在 ConfigureServices < Startup类的/ code>方法如下:
Now you have to make changes in ConfigureServices
method of the Startup class as follows:
services.AddDefaultIdentity<IdentityUser<int>>() // here replace `IdentityUser` with `IdentityUser<int>`
.AddDefaultUI()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
现在,最后您的模型配置(适用于Solution-1和Solution-2)使用 Fluent Api
应该如下:
Now finally your model configuration (for both Solution-1 and Solution-2) using Fluent Api
should be as follows:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<UserBook>()
.HasKey(ub => new { ub.UserId, ub.BookId });
modelBuilder.Entity<UserBook>()
.HasOne(ub => ub.ApplicationUser)
.WithMany(au => au.UserBooks)
.HasForeignKey(ub => ub.UserId);
modelBuilder.Entity<UserBook>()
.HasOne(ub => ub.Book)
.WithMany() // If you add `public ICollection<UserBook> UserBooks { get; set; }` navigation property to Book model class then replace `.WithMany()` with `.WithMany(b => b.UserBooks)`
.HasForeignKey(ub => ub.BookId);
}
这篇关于ASP Net Core:与IdentityUser添加多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!