我正在爲MVC 5項目使用EF 6(代碼第一次遷移)。在我本地的DEV機器上一切正常。Azure + EF 6 - 使代碼第一次遷移工作
但是當我部署我的項目天青,我收到以下錯誤,當我的應用程序首先嚐試與數據庫進行交互:
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.
我在Utilities.Data組裝我的EF相關的代碼,我的MVC項目在Utilities.Web程序集中。
這是我送給你的參考代碼:
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)
{
// This method will be called after migrating to the latest version.
// You can use the DbSet<T>.AddOrUpdate() helper extension method
// to avoid creating duplicate seed data. E.g.
//
// context.People.AddOrUpdate(
// p => p.FullName,
// new Person { FullName = "Andrew Peters" },
// new Person { FullName = "Brice Lambson" },
// new Person { FullName = "Rowan Miller" }
// );
//
}
}
Settings.cs
public static class Settings
{
public const string DB_CONNECTION_STRING = "DB.ConnectionString";
// other settings ...
public static string Get([Required] string key)
{
return CloudConfigurationManager.GetSetting(key);
}
}
而在App Settings
部我在本定義的Configuration
標籤:
鍵:DB.ConnectionString
值:數據源= TCP:host.database.windows.net, 1433; Initial Catalog = Utilities; User Id = user @ server; Password = pwd;
您是否在global.asax中添加了'DropCreateDatabaseIfModelChanges(){}'? –
Eiaddar
@ Eddy4v不,我沒有。我需要嗎?它不在那裏,它仍然在我的DEV圖像。 – Moon
即便如此,你必須使用更新的數據庫 來更新數據庫扔包管理器控制檯,並確保您的連接字符串名稱相同的語境名女巫是** ** UtilitiesContext – Eiaddar