我一直在对IdentityModels.cs进行一些更改。通常只是忽略几个字段并重命名表格,并且#34; AspNetUsers"用户"用户"。但是在我保存这些更改后,构建项目并生成迁移,它只是空的..
我对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行,即公共虚拟列表。
然后我还补充道:
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");
}
正如我所说,只是忽略一些字段并将表重命名为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");
}
}
编辑II
这是我的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中进行微小更改是解决方案。谢谢你的帮助
答案 0 :(得分:1)
问题在于OnModelCreating
方法
modelBuilder.Entity<IdentityUser>().
你引用 IdentityUser ,你应该使用 ApplicationUser
所以你的OnModelCreating
变成了
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");
}