Everything was working just fine
simply
the error means that your models has changes and is not in Sync with DB ,so
go to package manager console ,
add-migration foo2
this will give a hint of what is causing the issue ,
may be you removed something or in my case I remove a data annotation .
from there you can get the change and hopefully reverse it in your model.
after that delete foo2 .
This worked for me - no other changes required.
DELETE FROM [dbo].[__MigrationHistory]
When I am developing, I prefer to use this practical class to configure Migrations.
Hope it helps.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
this.Configuration.LazyLoadingEnabled = false;
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
Database.SetInitializer(new StackOverflowInitializer());
}
public class StackOverflowInitializer : DropCreateDatabaseIfModelChanges<ApplicationDbContext>
{
public StackOverflowInitializer()
{
// TODO NOTHING, COMMENT ALL
// IF CHANGES, RECREATE
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
// CREATE ONLY NOT EXITS
//Database.SetInitializer<Context>(new CreateDatabaseIfNotExists<ApplicationDbContext>());
}
}
public System.Data.Entity.DbSet<stackoverflow.Models.Company> Companies { get; set; }
}
this comes because you add some property to one of your model and you did not update-Database
. to solve this you have to remove it from model or you have to add-migration anyProperName
with that properties and Update-database
.
I know I am very late, but I want to give contribution too. This error is really strange, because the browser is not able to understand how the changes should be rendered because the classes and their properties might have changed but not committed to the database.
So do one thing,
create one migration in Package Manager Console(Tools > NuGet Package Manager > Package Manager Console) using this command:
add-migration UpdateMigration
where UpdateMigration is the name of your Migration. You can give it any name of your choice but please be specific.
After that, we just need to update the database, so run this:
update-database
Now that you have committed your changes to the database, just refresh your browser and there you go!
Hope this helps.
From the Tools menu, click NuGet Package Manger, then click Package Manager Console (PMC). Enter the following commands in the PMC.
Enable-Migrations Add-Migration Init Update-Database Run the application. The solution to the problem is from here