我遇到了这个让我生气很多天的问题。我编写了一个控制台应用程序,首先使用代码创建一个新数据库,然后使用迁移来更新它。它工作得很好。但是,当我编写一个 wcf 服务来做与控制台应用程序一样的事情时,它不能按我的意愿工作。问题是我无法使用代码更新数据库,但 PM 控制台上的命令 (Update-Database -Verbose) 可以做到。你能给我一些建议吗?这是我的代码:Configuration.cs
public Configuration()
{
AutomaticMigrationsEnabled = true;
AutomaticMigrationDataLossAllowed = true;
}
protected override void Seed(DatabaseInitialization.DatabaseModel 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" }
// );
//
}
迁移文件:
public partial class InitialCreate : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
主文件:
public static void InitializeService(DataServiceConfiguration config)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<DatabaseModel, DatabaseInitialization.Migrations.Configuration>());
InitializeDatabase();
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
public static void InitializeDatabase()
{
var context = new DatabaseModel();
context.Database.Initialize(true);
}
谢谢!