c# - 不支持每种类型的多个对象集。对象集 IdentityUsers' 和 Users 都可以包含类型的实例

标签 c# asp.net asp.net-mvc entity-framework asp.net-mvc-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; }
}

最佳答案

你的问题是这一行:

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

IdentityDbContext已经包含 Users类型为 IDbSet<ApplicationUser> 的属性.您不需要添加自己的 DbSet对于 ApplicationUser .删除该行就可以了。

关于c# - 不支持每种类型的多个对象集。对象集 IdentityUsers' 和 Users 都可以包含类型的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25549391/

相关文章:

c# - 该列在选择列表中无效,因为该列未包含在聚合函数或GROUP BY子句中

asp.net-mvc - ASP.NET MVC 中的 HTML 清理程序可过滤危险标记,但允许其余标记

c# - 为什么调用 View 时 View 中的嵌套对象为空。 ASP.Net MVC

c# - 获取该月下一个第 n 天的 DateTime

c# - 为 ListView Items_added 添加事件处理程序

c# - AutoMapper 将 ViewModel 中的 int[] 或 List<int> 映射到域模型中的 List<Type>

asp.net - 在局部 View 中强制使用无 Html.BeginForm/Ajax.BeginForm 的无障碍语法

c# - 使用 VSTO 在 Word 中动态创建的表格后插入文本

javascript - 如何为从 javascript 调用的网格应用 css?

c# - 无法将 linq 结果隐式转换为 View 模型列表