同一类型不支持多重对象集。对象集 IdentityUsers 和 Users 都可以包含类型实例。

4

对象集IdentityUsers和Users都可以包含类型为net的实例。

但是我尝试按照这个教程创建多对多关系:getting-started-with-ef-using-mvc

但是我得到了这个错误 => 不支持每个类型的多个对象集。 对象集“IdentityUsers”和“Users”都可以包含类型为“bugs_b_gone.Models.ApplicationUser”的实例。.

我在项目类中有一个属性,使创建项目的用户成为管理员。 但现在管理员必须创建用户并将其分配给项目。

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

现在是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; }
}
1个回答

11

你的问题出在这一行代码:

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

IdentityDbContext已经包含一个类型为IDbSet<ApplicationUser>Users属性。你不需要再添加自己的DbSet用于ApplicationUser。移除那行代码就可以了。


当我删除它时,它会在运行时重新出现。 - Keeper01
那么你有一些其他错误。但是你的问题所指的那个错误可以通过删除这行代码来解决。如果你需要帮助解决其他问题,请发布一个新问题。 - Chris Pratt
@ChrisPratt,但是在去除它之后,当我想为ApplicationUser添加Controller时,Model Class Box中没有显示Users Property。 - AminM
删除这行代码会导致构建错误,因为该属性在其他地方被使用。 - Martin Vaughan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接