本文介绍了不支持每个类型的多个对象集。对象设置IdentityUsers'and用户可以同时包含类型的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对象设置IdentityUsers'and用户可以同时包含键入net的实例

但我试图创建一个多对多的关系像本教程介绍了工具入​​门与 - 使用-EF-MVC

但我得到这个错误=>
每种类型的多个对象集,不支持。该对象集IdentityUsers和用户可以同时包含类型bugs_b_gone.Models.ApplicationUser 的实例

我在项目类的属性那么,谁创造了这个项目的用户是管理员
但现在,管理员有权创建用户,并将其分配到项目

 公共类项目
{
    [键]
    公众诠释专案编号{搞定;组; }
    //公众诠释AdminID {搞定;组; }
    [需要]
    [StringLength(50的ErrorMessage =标题不能超过50个字符。)
    [列(标题)
    [显示(名称=标题)
    公共字符串名称{搞定;组; }
    [需要]
    公共字符串描述{搞定;组; }
    //公众诠释MEMBERID {搞定;组; }    //公共虚拟ApplicationUser用户{搞定;组; }    公共虚拟的ICollection<错误>错误{搞定;组; }    // veel运veel
    公共虚拟的ICollection< ApplicationUser>用户{搞定;组; }
}

现在IdentityModel.cs

 公共类ApplicationUser:IdentityUser
{
    公共虚拟的ICollection<项目>项目{搞定;组; }
    公共虚拟的ICollection<错误>错误{搞定;组; }
    公共虚拟的ICollection<&评论GT;评论{搞定;组; }
}公共类ApplicationDbContext:IdentityDbContext< ApplicationUser>
{
    公共ApplicationDbContext()
        :基地(DefaultConnection)
    {
    }    公共DbSet<项目>项目{搞定;组; }
    公共DbSet<错误>错误{搞定;组; }
    公共DbSet<&评论GT;评论{搞定;组; }
    保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
    {
        base.OnModelCreating(模型构建器);
        modelBuilder.Entity< IdentityUserLogin>()HasKey<串GT(L => l.UserId);
        modelBuilder.Entity< IdentityRole>()HasKey<串GT(R = GT; r.Id);
        modelBuilder.Entity< IdentityUserRole方式>()HasKey(R = gt;新建{r.RoleId,r.UserId});
        //modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();        / * modelBuilder.Entity&LT; AssignedTo&GT;()
          .HasOptional(F =&GT; f.AssignedToID)
          .WithRequired(S =&GT; s.Bug); * /        modelBuilder.Conventions.Remove&LT; PluralizingTableNameConvention&GT;();        modelBuilder.Entity&LT;项目&GT;()
            .HasMany(C =&GT; c.User).WithMany(I =&GT; i.Projects)
            .MAP(T =&GT; t.MapLeftKey(专案编号)
                .MapRightKey(ID)
                .ToTable(ProjectUser));    }    公共System.Data.Entity.DbSet&LT; bugs_b_gone.Models.ApplicationUser&GT; IdentityUsers {搞定;组; }
}


解决方案

您的问题是这一行:

 公共System.Data.Entity.DbSet&LT; bugs_b_gone.Models.ApplicationUser&GT; IdentityUsers {搞定;组; }

IdentityDbContext 已经包含了一个用户与type属性 IDbSet&LT; ApplicationUser&GT; 。你并不需要添加自己的 DbSet ApplicationUser 。删除这一行,你是好。

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 adminBut 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; }
}
解决方案

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.

这篇关于不支持每个类型的多个对象集。对象设置IdentityUsers'and用户可以同时包含类型的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 20:16