加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 不支持每种类型的多个对象集.对象集IdentityUsers’和用户

发布时间:2020-12-15 23:44:16 所属栏目:百科 来源:网络整理
导读:对象集IdentityUsers’和用户都可以包含net类型的实例 但我试图创建一个多对多的关系,就像这个教程显示getting-started-with-ef-using-mvc 但我得到了这个错误= 不支持每种类型的多个对象集.对象集’IdentityUsers’和’Users’都可以包含’bugs_b_gone.Mode
对象集IdentityUsers’和用户都可以包含net类型的实例

但我试图创建一个多对多的关系,就像这个教程显示getting-started-with-ef-using-mvc

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

我在项目类中有一个属性,因此创建项目的用户是admin
但是现在管理员必须创建用户并将其分配给项目

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已包含类型为IDbSet< ApplicationUser>的Users属性.您不需要为ApplicationUser添加自己的DbSet.删除该行,你很好.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读