2017-01-05 97 views
2

我一直在對IdentityModels.cs進行一些更改。大多隻是忽略了幾個字段,並將表格從「AspNetUsers」重命名爲「用戶」。但經過我保存這些修改,構建項目,並生成遷移,這只是空..ASP.NET添加遷移生成對IdentityModels.cs更改的空遷移

我到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"); 
    } 

正如我所說的,只是忽略某些字段和表重命名爲用戶。 後來我剛纔保存的這一切,建成該項目,並鍵入

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中添加什麼才能讓它們聚集在一起。

我已經看過其他問題在這裏堆棧溢出,但我真的找不到解決方案。我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作出細微的變化是解決辦法。感謝您的幫助

回答

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"); 
} 
+0

我按照你所說的做了改變,但遷移仍然是空的..我只需要在變更之後構建,然後添加遷移的權利? –

+0

是的,沒錯。 如果自上次生成遷移以來數據庫沒有更改,則不會生成遷移。 – Alex

+0

遷移是在Migrations目錄中生成的,但它仍然是空的,就像之前那樣。 –