【发布时间】:2017-01-05 15:40:04
【问题描述】:
我一直在对 IdentityModels.cs 进行一些更改。通常只是忽略几个字段并将表从“AspNetUsers”重命名为“Users”。但是在我保存这些更改、构建项目并生成迁移之后,它只是空的..
我对 IdentityModels.cs 所做的更改如下:
public class ApplicationUser : IdentityUser
{
public virtual List<Bestelling> Bestellingen { get; set; }
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;
}
}
我这里基本上加了1行,public virtual List。
然后我还补充说:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().Ignore(c => c.EmailConfirmed)
.Ignore(c => c.PhoneNumber)
.Ignore(c => c.PhoneNumberConfirmed)
.Ignore(c => c.TwoFactorEnabled)
.Ignore(c => c.LockoutEnabled)
.Ignore(c => c.LockoutEndDateUtc);
modelBuilder.Entity<IdentityUser>().ToTable("Users");
}
正如我所说,只是忽略一些字段并将表重命名为用户。 之后我只是保存了所有内容,构建了项目并输入了
add-migration database-v2
然后
update-database
但是什么也没发生,因为迁移看起来像这样:
namespace Eindopdracht.Migrations
{
using System;
using System.Data.Entity.Migrations;
public partial class databasev2 : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
}
现在我在其他模型中添加了这个位:
public virtual DbSet<Klant> Klants { get; set; }
但我不知道要在 IdentityModels.cs 中添加什么以使它们融合在一起。
我在 Stack Overflow 上查看了其他问题,但我真的找不到解决方案。
编辑
我的 ApplicationDbContext() 的内容:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("name=DatabankEntities", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<ApplicationUser>().Ignore(c => c.EmailConfirmed)
.Ignore(c => c.PhoneNumber)
.Ignore(c => c.PhoneNumberConfirmed)
.Ignore(c => c.TwoFactorEnabled)
.Ignore(c => c.LockoutEnabled)
.Ignore(c => c.LockoutEndDateUtc);
modelBuilder.Entity<ApplicationUser>().ToTable("Users");
}
}
编辑二
这是我的 Configuration.cs:
namespace Eindopdracht.Migrations
{
using System;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Linq;
internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.DatabankBestellijn>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
}
protected override void Seed(Eindopdracht.Models.DatabankBestellijn context)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
}
我改了
internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.DatabankBestellijn>
位到:
internal sealed class Configuration : DbMigrationsConfiguration<Eindopdracht.Models.ApplicationDbContext>
而且它有效!但是我现在不能更新数据库,因为它说这些表已经存在,有没有办法强制它?
update-database -Force
似乎不是要走的路。
最终编辑
更改 configuration.cs 文件并在 IdentityModels.cs 中进行细微更改是解决方案。感谢您的帮助
【问题讨论】: