none
Multiple object sets per type are not supported. The object sets 'UserCollection' and 'Users' can both contain instances of type 'QiAiLab2.Models.ApplicationUser'.

    问题

  • 在程序包管理器控制台中 使用Enable-Migrations -ContextTypeName  QALabDbContext -Force老是报错误

    Multiple object sets per type are not supported. The object sets 'UserCollection' and 'Users' can both contain instances of type 'QiAiLab2.Models.ApplicationUser'.

    在QALabDbContext 、ApplicationUser也没有重复啊  ,求各位大神帮忙看看

    DBContext:

     public class QALabDbContext:IdentityDbContext<ApplicationUser>
        {
            public QALabDbContext()
                : base("QiAiLab2ContextConn", throwIfV1Schema: false)
            {
            }
            public static QALabDbContext Create()
            {
                return new QALabDbContext();
            }

            #region/**实体类**/
            public DbSet<News> News { get; set; }

            public DbSet<NewsType> NewsType { get; set; }

            public DbSet<Resource> Resources { get; set; }

            public DbSet<ResoureType> ResourceType { get; set; }

            public DbSet<ApplicationRole> RolesCollection { get; set; }

            public DbSet<ApplicationUser> UserCollection { get; set; }
            #endregion

            #region/**开发使使用数据库生成与播种**/
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                if (modelBuilder == null)
                {
                    throw new ArgumentNullException("modelBuilder is null");
                }
                base.OnModelCreating(modelBuilder);
                //Defining the keys and relations
                modelBuilder.Entity<ApplicationUser>().ToTable("T_Sys_Users");
                modelBuilder.Entity<ApplicationRole>().HasKey<string>(r => r.Id).ToTable("T_Sys_Roles");
                modelBuilder.Entity<ApplicationUser>().HasMany<ApplicationUserRole>((ApplicationUser u) => u.UserRoles);
                modelBuilder.Entity<ApplicationUserRole>().HasKey(r => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("T_Sys_AccountUserRoles");
            }

    ApplicationUser实体类:

       public class ApplicationUser:IdentityUser
        {
            public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
            {
                // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
                var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
                // Add custom user claims here
                return userIdentity;
            }
            public ICollection<ApplicationUserRole> UserRoles { get; set; }
        }

    使用程序包控制台生产数据库:

    PM> Enable-Migrations -ContextTypeName  QALabDbContext -Force
    Checking if the context targets an existing database...
    System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'UserCollection' and 'Users' can both contain instances of type 'QiAiLab2.Models.ApplicationUser'.
       在 System.Data.Entity.Internal.DbSetDiscoveryService.RegisterSets(DbModelBuilder modelBuilder)
       在 System.Data.Entity.Internal.LazyInternalContext.CreateModelBuilder()
       在 System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
       在 System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
       在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
       在 System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized()
       在 System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
       在 System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
       在 System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml)
       在 System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context)
       在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase)
       在 System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
       在 System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration migrationsConfiguration)
       在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()
       在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       在 System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
       在 System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
       在 System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(String language, String rootNamespace)
       在 System.Data.Entity.Migrations.EnableMigrationsCommand.<>c__DisplayClass2.<.ctor>b__0()
       在 System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
    Multiple object sets per type are not supported. The object sets 'UserCollection' and 'Users' can both contain instances of type 'QiAiLab2.Models.ApplicationUser'.

    2015年12月30日 4:40

答案