簡體   English   中英

如何在MVC中添加多對多關系的聯結表?

[英]How to add to junction table of many-to-many relationship in MVC?

我在 MVC 項目中添加和訪問聯結表時遇到問題。 目前,我有一個 ApplicationUsers 的 class 存儲在 AspNetUsers 默認表中。 此外,我有一個項目的 class 存儲在相應的表中。 用戶可以有多個項目,項目可以有多個用戶。 我無法從 controller 訪問聯結表,也無法執行以下操作: db.Projects.Users.add(...)而不會收到 DbSet 不包含用戶定義的錯誤消息。

這是我的 ApplicationUser class:

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; }
   }
}

這是我的項目 class:

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 string? Status { get; set; }

        public string? ClientCompany { get; set; }

        public string? ProjectLeader { get; set; }

        public 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());

        builder.Entity<ProjectModel>()
                .HasMany<ApplicationUser>(s => s.Users)
                .WithMany(c => c.Projects);
    }


}

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);
    }
}

最后,這是我收到錯誤消息的 controller :

using System.Diagnostics;
using Microsoft.AspNetCore.Mvc;
using IssueTracker.Models;
using Microsoft.AspNetCore.Authorization;
using IssueTracker.Areas.Identity.Data;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;

namespace IssueTracker.Controllers;

[Authorize]
public class HomeController : Controller
{
    private IssueTrackerIdentityDbContext db;
    private UserManager<ApplicationUser> userManager;
    private RoleManager<IdentityRole> roleManager;

    public HomeController(IssueTrackerIdentityDbContext db, UserManager<ApplicationUser> userManager,
        RoleManager<IdentityRole> roleManager)
    {
        this.db = db;
        this.userManager = userManager;
        this.roleManager = roleManager;
    }

    public IActionResult Index()
    {
        return View();
    }

    public IActionResult CreateProject()
    {
        return View();
    }

    [HttpPost]
    public IActionResult CreateProject(String Name, String Description, String Status,
        String ClientCompany, String ProjectLeader, List<string> Contributors)
    {
        var project = new ProjectModel
        {
            Name = Name,
            Description = Description,
            Status = Status,
            ClientCompany = ClientCompany,
            ProjectLeader = ProjectLeader
        };

        db.Projects.Add(project);
        db.Projects.Users.Add(ProjectLeader);
        return View();
    }

}


我不確定如何添加到聯結表以將用戶正確鏈接到項目。 如果我的多對多關系設置有問題,或者是否有辦法修復錯誤消息,請告訴我。 謝謝!!

您可以嘗試如下代碼:

 var project = new ProjectModel
        {
            Name = Name,
            Description = Description,
            Status = Status,
            ClientCompany = ClientCompany,
            ProjectLeader = ProjectLeader
        };

    //db.Projects.Add(project);
   // db.Projects.Users.Add(ProjectLeader);
      var User = new ApplicationUser()
        {
            FirstName="a"
        };
        project.Users.Add(User);
        db.Projects.Add(project);

閱讀此答案以了解更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM