将用户分配到 Asp.net Mvc 5应用程序中的项目列表

本文关键字:应用程序 项目 列表 Mvc 分配 用户 Asp net | 更新日期: 2023-09-27 18:35:21

我想将用户分配到我的项目,但是运行它时出现问题,我收到错误

不支持每种类型有多个对象集。对象集"ApplicationUsers"和"Users"都可以包含类型为"ProjectManagementSystem.Models.ApplicationUser"的实例。这是我的身份模型.cs

public class ApplicationUser
    : IdentityUser<string, ApplicationUserLogin,
    ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationUser()
    {
        this.Id = Guid.NewGuid().ToString();
    }

    public async Task<ClaimsIdentity>
        GenerateUserIdentityAsync(ApplicationUserManager manager)
    {
        var userIdentity = await manager
            .CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        return userIdentity;
    }
    public string Name { get; set; }
    public string LastName { get; set; }
    public string User_name { get; set; }
    public string City { get; set; }
    public virtual ICollection<Project> Projects { get; set; }
}

这也是来自IdentityModels.cs

modelBuilder.Entity<Project>()
         .HasMany<ApplicationUser>(c => c.UsersOnProjects).WithMany(i => i.Projects)
         .Map(t => t.MapLeftKey("ProjectID")
             .MapRightKey("Id")
             .ToTable("Projects"));

而这个是为了:

    public System.Data.Entity.DbSet<ProjectManagementSystem.Models.Clients> Clients { get; set; }
    public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ProjectCategory> 
                                            ProjectCategories { get; set; }
    public System.Data.Entity.DbSet<ProjectManagementSystem.Models.Project> Projects
                                                              { get; set; }
    public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ApplicationUser> 
                                                       ApplicationUsers { get; set; }

现在这是我的项目模型

  public class Project
  {
    [Key]
    public int ProjectId { get; set; }
    [Required]
    [Display(Name = "Project Name")]
    public string ProjectTitle { get; set; }
    [Required]
    public string Description { get; set; }
    public string Url { get; set; }
    public int ProjectCategoryId { get; set; }
    [Required]
    [Display(Name = "Project Category")]
    public virtual ProjectCategory ProjectCategory { get; set; }
    public int ClientId { get; set; }
    public virtual Clients Client { get; set; }
    public string Budget { get; set; }
    [Display(Name = "Start Date")]
    public string StartDate { get; set; }
    [Display(Name = "End Date")]
    public string EndDate { get; set; }
    public string UserId { get; set; }
    public virtual ApplicationUser User { get; set; }
    public virtual ICollection<ApplicationUser> UsersOnProjects { get; set; }
 }

我尝试了一切,例如删除此部分

 public System.Data.Entity.DbSet<ProjectManagementSystem.Models.ApplicationUser> 
                                               ApplicationUsers { get; set; }

更改我的项目控制器

  ViewBag.UserId = new SelectList(db.ApplicationUsers, "Id", "Name");
  To
 ViewBag.UserId = new SelectList(db.Users, "Id", "Name");

还有更多,但我总是得到同样的错误......我已从此链接创建了用户角色和组http://typecastexception.com/post/2014/08/10/ASPNET-Identity-20-Implementing-Group-Based-Permissions-Management.aspx

也许这有什么东西给了我错误。我正在使用实体框架创建我的控制器

将用户分配到 Asp.net Mvc 5应用程序中的项目列表

我已经通过将这一行放在我的视图中来解决它

@Html.ValidationSummary(false)

并将 var modelStateErrors 和断点放在 if (ModelState.IsValid) 上

var modelStateErrors = this.ModelState.Values.SelectMany(m => m.Errors);
        if (ModelState.IsValid)
        {
            db.Projects.Add(project);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

之后,我发现我的项目模型中有错误消息,并且项目类别是必需的。我删除了它,现在它确实有效....

public int ProjectCategoryId { get; set; }
    [Display(Name = "Project Category")]
    public virtual ProjectCategory ProjectCategory { get; set; }