我正在尝试使用代码优先的方法将表和数据迁移到我现有的数据库。
当我运行Add-Migration [somename]时,我最终得到以下例外:
Cannot find the object "dbo.AspNetUserRoles" because it does not exist or you do not have permissions.
现在这里的交易,我的表被命名为aspnet_Users, aspnet_UserRoles等。如您所见,我生成的迁移文件将表名大写。我尝试手动重命名这些,不幸的是没有任何进展。我似乎弄不明白是什么原因造成的。
我的数据库实体是这样的:
public partial class MyDBContext : IdentityDbContext<ApplicationUser>
{
public FunkaDbContext()
: base("name=MyDBContext", throwIfV1Schema: false)
{
Database.SetInitializer<MyDBContext>(new DropCreateDatabaseIfModelChanges<MyDBContext>());
}
public virtual DbSet<educationSignup> EducationSignups { get; set; }
public virtual DbSet<ParticipantsInformation> ParticipantsInformations { get; set; }
public virtual DbSet<PaymentInformation> PaymentInformations { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<educationSignup>()
.HasMany(e => e.participantsInformations)
.WithRequired(e => e.educationSignup)
.HasForeignKey(e => e.SignupId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<educationSignup>()
.HasMany(e => e.paymentInformations)
.WithRequired(e => e.educationSignup)
.HasForeignKey(e => e.SignupId)
.WillCascadeOnDelete(false);
modelBuilder.Entity<IdentityUser>().HasKey<string>(u => u.Id);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
}
public static MyDbContext Create()
{
return new MyDbContext();
}
}
删除db中的迁移历史,并为您的上下文使用底部映射
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
modelBuilder.Entity<IdentityUserRole>()
.HasKey(r => new { r.UserId, r.RoleId })
.ToTable("AspNetUserRoles");
modelBuilder.Entity<IdentityUserLogin>()
.HasKey(l => new { l.LoginProvider, l.ProviderKey, l.UserId })
.ToTable("AspNetUserLogins");