I'm developing a web application in ASP.NET using code first migrations. It works fine locally but after deploying to Azure, code first migrations are not executed. I have been following this tutorial step by step a few times but I have not been able to spot what is wrong on my setup. Here is the relevant code:
DB Context:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false) {}
public DbSet<BC_Instance> BiocloudInstances { get; set; }
static ApplicationDbContext() {}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var conv = new AttributeToTableAnnotationConvention<SoftDeleteAttribute, string>(
"SoftDeleteColumnName",
(type, attributes) => attributes.Single().ColumnName);
modelBuilder.Conventions.Add(conv);
}
}
Connection strings:
(it is replaced on publish, but just in case)
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=bcplatform2;Integrated Security=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" /></connectionStrings>
Code First Migrations configuration
internal sealed class Configuration : DbMigrationsConfiguration<bcplatform2.Models.ApplicationDbContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}
protected override void Seed(bcplatform2.Models.ApplicationDbContext context)
{
var userManager = new ApplicationUserManager(new UserStore<ApplicationUser>(context));
var roleManager = new ApplicationRoleManager(new RoleStore<ApplicationRole>(context));
const string name = {name here};
const string password = {pass here};
const string adminRole = {role};
string[] roles = new string[] { adminRole, ApplicationRole.DefaultRoleName };
foreach (string role in roles)
{
if (!context.Roles.Any(r => r.Name == role))
{
roleManager.CreateAsync(new ApplicationRole(role));
}
}
if (!context.Users.Any(u => u.UserName == name))
{
var user = new ApplicationUser { UserName = name, Email = name, credit = 10 };
userManager.Create(user, password);
userManager.AddToRole(user.Id, adminRole);
userManager.SetLockoutEnabled(user.Id, false);
}
}
}
Entity framework section in deployed Web.config
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
<contexts>
<context type="bcplatform2.Models.ApplicationDbContext, bcplatform2">
<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2[[bcplatform2.Models.ApplicationDbContext, bcplatform2], [bcplatform2.Migrations.Configuration, bcplatform2]], EntityFramework, PublicKeyToken={token}">
<parameters>
<parameter value="DefaultConnection_DatabasePublish" />
</parameters>
</databaseInitializer>
</context>
</contexts>
</entityFramework>
Connection strings in deployed Web.config
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=tcp:{serverid}.database.windows.net,1433;Initial Catalog={dbid};User Id={user};Password={password}" providerName="System.Data.SqlClient" />
<add name="DefaultConnection_DatabasePublish" connectionString="Data Source=tcp:{serverid}.database.windows.net,1433;Initial Catalog={dbid};User ID={user};Password={password}" providerName="System.Data.SqlClient" />
</connectionStrings>
<parameter value="mssqllocaldb" />
or<databaseInitializer type="System.Data.Entity.MigrateDatabaseToLatestVersion`2
Do you know if that is the way it should be? – nest Feb 8 '16 at 9:15