Everything was working just fine then I tried to update one of my model classes ( the App class and the update is now left commented ) which I will be listing below; and boom I had this ugly error.
The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269). at System.Data.Entity.CreateDatabaseIfNotExists
1.InitializeDatabase(TContext context) at System.Data.Entity.Internal.InternalContext.<>c__DisplayClassf
1.b__e() at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() at System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c) at System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action
1 action) at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet
1.Include(String path) at System.Data.Entity.Infrastructure.DbQuery1.Include(String path) at System.Data.Entity.QueryableExtensions.Include[T](IQueryable
1 source, String path) at System.Data.Entity.QueryableExtensions.Include[T,TProperty](IQueryable1 source, Expression
1 path) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.GetUserAggregateAsync(Expression
1 filter) at Microsoft.AspNet.Identity.EntityFramework.UserStore6.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager
2.FindByNameAsync(String userName) at Microsoft.AspNet.Identity.UserManager`2.d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at ControlPanel.Web.Controllers.AccountController.d__2.MoveNext() in d:\Projects\FULL\Control Panel\ControlPanel.Web\Controllers\AccountController.cs:line 56
At first I thought it might be a migrations problem, so I dropped the database entirely, re-enabled the migrations, and added an Init migration and updated the database using
update-database -force -verbose
Everything goes well with no complaints, however, whenever I try to log in to my site I get the previous error. I did the migration thing about ten times without being able to solve the problem.
Here are my domain classes ( models ):
public class App
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int AppId { get; set; }
//[Required]
public virtual string FacebookId { get; set; }
//[Required]
public virtual string Secret { get; set; }
public virtual List<User> Users { get; set; }
public virtual List<Post> Posts { get; set; }
//public virtual ApplicationUser Admin { get; set; }
}
public class Post
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int PostId { get; set; }
public virtual string Content { get; set; }
public virtual string Link { get; set; }
public virtual string Image { get; set; }
public virtual bool IsSpecial { get; set; }
//[Required]
public virtual App App { get; set; }
//[Required]
public virtual DateTime? PublishDate { get; set; }
}
public class User
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int UserId { get; set; }
[MaxLength(500)]
public virtual string FacebookId { get; set; }
[MaxLength(500)]
public virtual string Token { get; set; }
//[Required]
public virtual App App { get; set; }
}
Here are my IdentityModels:
public class ApplicationUser : IdentityUser
{
public virtual List<App> Apps { get; set; }
public bool? IsPremium { get; set; }
[DataType(DataType.Date)]
public DateTime? LastPublishDateTime { get; set; }
}
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("dCon")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUser>().ToTable("Admins");
modelBuilder.Entity<ApplicationUser>().ToTable("Admins");
modelBuilder.Entity<IdentityUserRole>().ToTable("AdminRoles");
modelBuilder.Entity<IdentityUserLogin>().ToTable("Logins");
modelBuilder.Entity<IdentityUserClaim>().ToTable("Claims");
modelBuilder.Entity<IdentityRole>().ToTable("Roles");
}
}
Just in case anyone else stumbles upon this that was doing a database first implementation like me.
I made a change by extending the ApplicationUser
class, adding a new field to the AspNetUsers
table, and then had this error on startup.
I was able to resolve this by deleting the record created in the __MigrationHistory
table (there was only one record there) I assume EF decided that I needed to update my database using the migration tool - but I had already done this manually myself.