EntitySet' AspNetRoles'使用架构&#d;'和表#AspNetRoles'已经定义了。每个EntitySet必须引用唯一的模式和表

时间:2014-11-19 09:51:17

标签: entity-framework asp.net-identity ef-migrations

迁移错误:

  

PM> enable-migrations检查上下文是否以现有为目标   数据库...   System.Data.Entity.ModelConfiguration.ModelValidationException:一个或   在模型生成期间检测到更多验证错误:

     

AspNetRoles:名称:具有架构'dbo'的EntitySet'AspNetRoles'和   表'AspNetRoles'已经定义。每个EntitySet必须引用   一个独特的架构和表格。

     

at System.Data.Entity.Core.Metadata.Edm.EdmModel.Validate()at at   System.Data.Entity.DbModelBuilder.Build(DbProviderManifest   providerManifest,DbProviderInfo providerInfo)at   System.Data.Entity.DbModelBuilder.Build(的DbConnection   providerConnection)   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext   internalContext)at   System.Data.Entity.Internal.RetryLazy 2.GetValue(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
at System.Data.Entity.Internal.InternalContext.CreateObjectContextForDdlOps() at System.Data.Entity.Database.Exists() at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext
1.IsIdentityV1Schema(的DbContext   db)at   Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext 1..ctor(String nameOrConnectionString, Boolean throwIfV1Schema) at Microsoft.AspNet.Identity.EntityFramework.IdentityDbContext 1..ctor(字符串   nameOrConnectionString)在Apk.Model.ApkEntities..ctor()中   c:\ Users \ Administrator \ Documents \ Visual Studio   2013 \ Projects \ Apk \ Apk.Data \ ApkEntities.cs:第10行   ---从抛出异常的先前位置开始的堆栈跟踪结束--- at   System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()at   System.Data.Entity.Infrastructure.DbContextInfo.CreateInstance()at   System.Data.Entity.Infrastructure.DbContextInfo..ctor(类型   contextType,DbProviderInfo modelProviderInfo,AppConfig config,   DbConnectionInfo connectionInfo,Func`1 resolver)at   System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration   配置,DbContext usersContext,DatabaseExistenceState   presenceState)at   System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration   配置)   System.Data.Entity.Migrations.Design.MigrationScaffolder..ctor(DbMigrationsConfiguration   migrationsConfiguration)at   System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldRunner.Run()   在System.AppDomain.DoCallBack(CrossAppDomainDelegate   callBackDelegate)at   System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
  在System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner)   跑者)   System.Data.Entity.Migrations.Design.ToolingFacade.ScaffoldInitialCreate(字符串   language,String rootNamespace)at   System.Data.Entity.Migrations.EnableMigrationsCommand<> c__DisplayClass2< .ctor> b__0()   在   System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(动作   命令)在模型期间检测到一个或多个验证错误   代:

     

AspNetRoles:名称:具有架构'dbo'的EntitySet'AspNetRoles'和   表'AspNetRoles'已经定义。每个EntitySet必须引用   一个独特的架构和表格。

为什么会出现此错误?

我需要更改IdentityRole和IdentityUser。

 public class ApkEntities : IdentityDbContext<ApplicationUser>
    {
        public ApkEntities ()
            : base("ApkEntities")
        {
        }

        public virtual void Commit()
        {
            base.SaveChanges();
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Configurations.Add(new AspNetRoleConfiguration());
            modelBuilder.Configurations.Add(new AspNetUserConfiguration());
        }
    }



 public class AspNetRoleConfiguration : EntityTypeConfiguration<ApplicationRole>
    {
        public AspNetRoleConfiguration(string schema = "dbo")
        {
            ToTable(schema + ".AspNetRoles");
            HasKey(x => x.Id);
            Property(x => x.Id).HasColumnName("Id").IsRequired().HasMaxLength(128).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            Property(x => x.Name).HasColumnName("Name").IsRequired().HasMaxLength(256);
            Property(x => x.NameFa).HasColumnName("NameFa").IsRequired().HasMaxLength(50);
            Property(x => x.Description).HasColumnName("Description").IsOptional().HasMaxLength(250);
            Property(x => x.IsActivated).HasColumnName("IsActivated").IsRequired();
        }
    }

 public class AspNetUserConfiguration : EntityTypeConfiguration<ApplicationUser>
    {
        public AspNetUserConfiguration(string schema = "dbo")
        {
            ToTable(schema + ".AspNetUsers");
            HasKey(x => x.Id);
            Property(x => x.Id).HasColumnName("Id").IsRequired().HasMaxLength(128).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
            Property(x => x.Email).HasColumnName("Email").IsOptional().HasMaxLength(256);
            Property(x => x.PhoneNumberConfirmed).HasColumnName("PhoneNumberConfirmed").IsOptional();
            Property(x => x.FirstName).HasColumnName("FirstName").IsOptional();
            Property(x => x.LastName).HasColumnName("LastName").IsOptional();
            Property(x => x.DateCreated).HasColumnName("DateCreated").IsRequired();
            Property(x => x.PasswordHash).HasColumnName("PasswordHash").IsOptional();
            Property(x => x.SecurityStamp).HasColumnName("SecurityStamp").IsOptional();
            Property(x => x.UserName).HasColumnName("UserName").IsRequired().HasMaxLength(256);
        }
    }



     public class ApplicationRole : IdentityRole
        {
            public string Description { get; set; }
            public string NameFa { get; set; }
            public bool IsActivated { get; set; }
        }

     public class ApplicationUser : IdentityUser
        {
            public ApplicationUser()
            {
                DateCreated = DateTime.Now;
                LF_FavoraiteTeam = new Collection<LF_FavoraiteTeam>();
            }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime DateCreated { get; set; }
            public DateTime? LastLoginTime { get; set; }
            public bool? IsActivated { get; set; }
            public virtual ICollection<LF_FavoraiteTeam> LF_FavoraiteTeam { get; set; }

}

1 个答案:

答案 0 :(得分:1)

尝试在ApkEntities上下文中显式定义ApplicationUser和ApplicationRole DbSets,并删除IdentityRole和IdentityUser对象中已有的属性。已经通过继承IdentityDbContext来定义。