I’ve been reading about and beginning to use Entity Framework Code-First.
Its been working fine on my local machine (DefaultConnection), but it seems to have stopped working on my production server (a SQL Server add-on from AppHarbor).
One thing I noticed is that the __MigrationHistory folder is not hidden as a system table.
I’ve been using the method for automatic migrations described on AppHarbor’s blog.
This is my DatabaseContext class:
public class DatabaseContext : DbContext {
public DatabaseContext() : base("DefaultConnection") { }
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Challenge> Challenges { get; set; }
public DbSet<Feedback> Feedbacks { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseContext, Configuration>());
}
}
I would like to get code-first working again on the appharbor database. Is there a way to re-enable it, or should I delete all of the generated migration classes and __MigrationHistory and re-create the database by temporarily changing the OnModelCreating constructor to use a DropCreateDatabaseAlways initializer? What would be a better way of resetting the migrations and starting them from the current written code?
Code First Migrations is just looking for a table called __MigrationHistory. It doesn’t care whether it is marked as a system table or not.
In the guide you are referring to they are using a SQL Server CE, and if you are using that provider, Code First will never mark it as a system table.
On your development machine you are probably using a different version of SQL Server, so there you see it as a system table.
The reason it is marked as a system table in some databases, is simply to hide it. Migrations doesn’t require it to be a system table in order to work.
You can read more in this blog post by Arthur Vickers from the Entity Framework team, where he shows how to make __MigrationHistory become a non-system table on Sql Server.