如何使用自定义ApplicationUser类MVC创建多对多关系



我需要链接两个表(使用自定义ApplicationUser类的AspNetUsers和一个自定义Projects类(。我希望用户有多个项目,反之亦然。我已经将以下代码添加到我的两个模型中;但是,在执行迁移和更新数据库时,不会创建任何链接表。

这是我的ApplicationUser类:

using System;
using System.Security.Claims;
using IssueTracker.Models;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
namespace IssueTracker.Areas.Identity.Data
{
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
this.Projects = new HashSet<ProjectModel>();
}
public String? FirstName { get; set; }
public String? LastName { get; set; }
public String? Role { get; set; }
public virtual ICollection<ProjectModel> Projects { get; set; }
}
}

这是我的项目模型类:

using System;
using Microsoft.AspNetCore.Identity;
using IssueTracker.Areas.Identity.Data;
using System.ComponentModel.DataAnnotations;
namespace IssueTracker.Models
{
public class ProjectModel
{
public ProjectModel()
{
this.Users = new HashSet<ApplicationUser>();
}
public int Id { get; set; }
public string? Name { get; set; }
public string? Description { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
}

这是我的DbContext:

using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using IssueTracker.Models;
namespace IssueTracker.Areas.Identity.Data;
public class IssueTrackerIdentityDbContext : IdentityDbContext<ApplicationUser>
{
public DbSet<ProjectModel> Projects { get; set; }
public IssueTrackerIdentityDbContext()
{
}
public IssueTrackerIdentityDbContext(DbContextOptions<IssueTrackerIdentityDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
// Customize the ASP.NET Identity model and override the defaults if needed.
// For example, you can rename the ASP.NET Identity table names and more.
// Add your customizations after calling base.OnModelCreating(builder);
builder.ApplyConfiguration(new ApplicationUserEntityConfiguration());
}

}
public class ApplicationUserEntityConfiguration : IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
builder.Property(u => u.FirstName).HasMaxLength(255);
builder.Property(u => u.LastName).HasMaxLength(255);
}
}

在用上面的代码运行了一个迁移之后,我得到了这个迁移,它不会创建一个表来链接两者。

using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace IssueTracker.Migrations
{
public partial class UserProjectTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.RenameColumn(
name: "ID",
table: "Projects",
newName: "Id");
migrationBuilder.AddColumn<string>(
name: "ApplicationUserId",
table: "Projects",
type: "TEXT",
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Projects_ApplicationUserId",
table: "Projects",
column: "ApplicationUserId");
migrationBuilder.AddForeignKey(
name: "FK_Projects_AspNetUsers_ApplicationUserId",
table: "Projects",
column: "ApplicationUserId",
principalTable: "AspNetUsers",
principalColumn: "Id");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Projects_AspNetUsers_ApplicationUserId",
table: "Projects");
migrationBuilder.DropIndex(
name: "IX_Projects_ApplicationUserId",
table: "Projects");
migrationBuilder.DropColumn(
name: "ApplicationUserId",
table: "Projects");
migrationBuilder.RenameColumn(
name: "Id",
table: "Projects",
newName: "ID");
}
}
}

我应该如何更新我的代码,以便在ApplicationUser和Projects之间正确地创建多对多关系。此外,一旦创建了此关系,我如何正确访问和修改该关系。无论何时将用户添加到项目中,我都需要更新所有3个类(反之亦然(,还是只需要更新链接表。非常感谢!我是MVC的新手,所以如果这是非常初级的,请原谅我。

也试试这个:

public class ApplicationUserEntityConfiguration : 
IEntityTypeConfiguration<ApplicationUser>
{
public void Configure(EntityTypeBuilder<ApplicationUser> builder)
{
builder.Property(u => u.FirstName).HasMaxLength(255);
builder.Property(u => u.LastName).HasMaxLength(255);

builder.HasMany(x => x.Projects).WithMany(x => x.Users);
}
}

最新更新