Hey all,
Am stuck with some code first migrations to Azure. Have Googled but not
found anything that seems to help.
Basically I've got the local stuff working, I understand the workflow. ie
Add-Migration name generates the code to upgrade the recent changes to the
model, and Upgrade-Database applies them to the local database.
What I can't figure out is how to tell Azure that I want it to drop my
database and start over. I've got that part working locally but when I
deploy to Azure it just ignores it.
Some of my current code (trying various things so this might not be the
best working example right now)
I have this in my Application_start
Database.SetInitializer(new DatabaseSeeder());
//Database.SetInitializer(new
MigrateDatabaseToLatestVersion<DomainDataContext, Configuration>()); //
tried this
using (var db = new TotalPlayerKill.Models.DomainDataContext())
{
db.Database.Initialize(true);
}
public class DatabaseSeeder :
DropCreateDatabaseAlways<Models.DomainDataContext>
{
public DatabaseSeeder()
{
Database.SetInitializer<DomainDataContext>(null);
try
{
using (var context = new DomainDataContext())
{
if (context.Database.Exists())
{
((IObjectContextAdapter)context).ObjectContext.DeleteDatabase();
((IObjectContextAdapter)context).ObjectContext.CreateDatabase();
}
}
}
catch (Exception ex)
{
throw new InvalidOperationException("The
((IObjectContextAdapter)context).ObjectContext.DeleteDatabase() database
could not be initialized.", ex);
}
}
my Configuration class has these in the constructor too:
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
Interestingly the catch in that try doesn't seem to fail, what does throw
an exception is when I try to access anything in the new model (that doesnt
exist because it wasn't migrate) thats when I see the exception.
I'm missing something. I'm going to look at the streaming logs while I run
this tonight but just thought I'd post this in case anyone has some good
resources on Azure that covers the more advanced Azure stuff.
I could always run some SQL directly on the db and drop the tables manually
but I was under the impression this could all be done via the various Azure
migration helpers/classes. (ie why doesn't my DropCreateDatabaseAlways
class Drop always???!)