I have two projects each with their own DbContext class that write to the same SQL Server 2012 database.
The DbContext classes are of the form:
public class BlogDbContext : DbContext
{
public BlogDbContext()
: base("CodeFirstTestConnString")
{
}
public BlogDbContext(string connectionString)
: base(connectionString)
{
}
public DbSet<Blog> Blogs { get; set; }
public DbSet<Post> Posts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//modelBuilder.HasDefaultSchema("blogtest");
}
}
Using Code First Migrations I can create my tables in the (existing) database by executing the following in each project:
Enable-Migrations
Add-Migration Initial
Update-Database
This works fine, however if I add in the line commented out above to set the default schema (both projects use the same schema), Update-Database fails in the second project with the error "There is already an object named '__MigrationHistory' in the database.".
By running "Update-Database -Verbose" I can see that with the default schema changed, "CREATE TABLE [blogtest].[__MigrationHistory]" is executed for the second project; if I don't change the default schema it only tries to create this table the first time.
Is this a bug in Entity Framework, or am I missing something?