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

每个类型不支持多个对象集。对象集“ ApplicationUsers”和“ Users”都可以包含“ ProjectManagementSystem.Models.ApplicationUser”类型的实例。
这是在我的IdentityModels.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; }


更改我的ProjectController

  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

也许有些东西给我错误。
我正在使用Entityframwork创建我的控制器

最佳答案

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

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


之后,我发现我的项目模型中有错误消息,并且需要ProjectCategory。我删除了它,现在它可以工作了。

public int ProjectCategoryId { get; set; }

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

关于c# - 将用户分配给Asp.net Mvc 5应用程序中的项目列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27649044/

10-12 02:02