I am using EF 6 (migration code first) for the MVC 5 project. Everything works fine on my local DEV machine.
But when I deploy my project to Azure, I get the following error when my application first tries to interact with the database:
Migrations is enabled for context 'UtilitiesContext' but the database does not exist or
contains no mapped tables. Use Migrations to create the database and its tables, for
example by running the 'Update-Database' command from the Package Manager Console.
I have EF related code in the Utilities.Data assembly and my MVC project in the Utilities.Web assembly.
Here is my code for your reference:
UtilitiesContext.cs
public sealed partial class UtilitiesContext : DbContext
{
public UtilitiesContext() : base(Settings.Get(Settings.DB_CONNECTION_STRING)) { }
public DbSet<PreLaunchSubscriber> PreLaunchSubscribers { get; set; }
private void SetCreatedAtUpdatedAt()
{
foreach (DbEntityEntry entityEntry in ChangeTracker.Entries())
{
switch (entityEntry.State)
{
case EntityState.Added:
((IEntity) entityEntry.Entity).CreatedAt = DateTime.Now;
break;
case EntityState.Modified:
((IEntity) entityEntry.Entity).UpdatedAt = DateTime.Now;
break;
}
}
}
[HandleException]
public override int SaveChanges()
{
SetCreatedAtUpdatedAt();
return base.SaveChanges();
}
[HandleException]
public override Task<int> SaveChangesAsync()
{
SetCreatedAtUpdatedAt();
return base.SaveChangesAsync();
}
}
Configuration.cs
internal sealed class Configuration : DbMigrationsConfiguration<UtilitiesContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = true;
ContextKey = "Utilities.Data.Contexts.UtilitiesContext";
}
protected override void Seed(UtilitiesContext context)
{
}
}
Settings.cs
public static class Settings
{
public const string DB_CONNECTION_STRING = "DB.ConnectionString";
public static string Get([Required] string key)
{
return CloudConfigurationManager.GetSetting(key);
}
}
And I have it defined in the section App Settingsfor the tab Configuration:
Key: DB.ConnectionString
Value: Data Source = tcp: host.database.windows.net, 1433; Start Directory = Utilities; User ID = user @ server; Password = pwd;