4

The object sets IdentityUsers'and Users can both contain instances of type net

But I tried to create a many to many relation like this tutorial shows getting-started-with-ef-using-mvc

But I got this error => Multiple object sets per type are not supported. The object sets 'IdentityUsers' and 'Users' can both contain instances of type 'bugs_b_gone.Models.ApplicationUser'.

I have a property in the project class so the user who has created the project is the admin But now that admin has to create users and assign it to the project

public class Project
{
    [Key]
    public int ProjectID { get; set; }
    //public int AdminID { get; set; }
    [Required]
    [StringLength(50, ErrorMessage = "Title cannot be longer than 50 characters.")]
    [Column("Title")]
    [Display(Name = "Title")]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }


    //public int MemberID { get; set; }

    //public virtual ApplicationUser User { get; set; }

    public virtual ICollection<Bug> Bugs { get; set; }

    // veel op veel
    public virtual ICollection<ApplicationUser> User { get; set; }
}

Now the IdentityModel.cs

public class ApplicationUser : IdentityUser
{
    public virtual ICollection<Project> Projects { get; set; }
    public virtual ICollection<Bug> Bugs { get; set; }
    public virtual ICollection<Comment> Comments { get; set; }
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    public DbSet<Project> Projects { get; set; }
    public DbSet<Bug> Bugs { get; set; }
    public DbSet<Comment> Comments { get; set; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });


        //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

        /*  modelBuilder.Entity<AssignedTo>()
          .HasOptional(f => f.AssignedToID)
          .WithRequired(s => s.Bug);*/

        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

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

    }

    public System.Data.Entity.DbSet<bugs_b_gone.Models.ApplicationUser> IdentityUsers { get; set; }
}
Khan
  • 344
  • 1
  • 7
  • 21
Keeper01
  • 293
  • 2
  • 4
  • 12

1 Answers1

11

Your problem is this line:

public System.Data.Entity.DbSet<bugs_b_gone.Models.ApplicationUser> IdentityUsers { get; set; }

IdentityDbContext already contains a Users property with type IDbSet<ApplicationUser>. You don't need to add your own DbSet for ApplicationUser. Remove that line and you're good.

Chris Pratt
  • 232,153
  • 36
  • 385
  • 444
  • when I delete it it comes back on run time – Keeper01 Aug 28 '14 at 14:17
  • Then you have some *other* error. But the one your question is about is fixed by removing this line. Post a new question with your other error if you need help figuring that one out. – Chris Pratt Aug 28 '14 at 14:19
  • @ChrisPratt but after remove it,when i want add Controller for ApplicationUser the Users Property not show in Model Class Box – AminM Feb 06 '17 at 14:18
  • Deleting this line then causes build errors as the property is used elsewhere. – Martin Vaughan Apr 27 '23 at 11:16