gpt4 book ai didi

c# - MigrateDatabaseToLatestVersion 没有运行 Seed() 方法

转载 作者:太空狗 更新时间:2023-10-29 17:51:43 25 4
gpt4 key购买 nike

我正在尝试自动生成我的数据库(如果它不存在)并运行 Seed() 方法来填充数据。在我的数据库上下文构造函数中,我有这个:

Database.SetInitializer(new MigrateDatabaseToLatestVersion<MyDBContext, Configuration>());

效果很好,我的数据库自动创建了我想要的所有表,但似乎没有调用 Seed() 方法,我的数据库是空的。这是我的课:

internal sealed class Configuration : DbMigrationsConfiguration<Context.MyDBContext>
{

public Configuration()
{
AutomaticMigrationsEnabled = true;
}

protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);

base.Seed(context);
}
}

当我在 Nuget 控制台中运行 Update-Database 时,数据会在数据库创建后填充,但是使用 MigrateDatabaseToLatestVersion Seed () 方法未被调用。

会发生什么?我尝试手动运行来自 here 的迁移:

var configuration = new MyDbContextConfiguration();
configuration.TargetDatabase = new DbConnectionInfo(
database.ConnectionString, database.ProviderName);

var migrator = new DbMigrator(configuration);
migrator.Update();

但也不行。


编辑:

好的,经过更多测试我发现 Seed() 方法运行但仅当数据库已经存在时,即在第一次创建数据库时运行Seed() 方法没有被执行,但是当我第二次运行我的应用程序时 Seed() 被执行了。我还必须添加 context.SaveChanges() 才能使其正常工作(感谢评论中的@DavidG):

protected override void Seed(Context.MyDBContext context)
{
context.Users.AddOrUpdate(
new Entities.User() { Email = "default@default.com", Password = "", Language = "en", CreatedDate = DateTime.Now }
);

context.SaveChanges();
base.Seed(context);
}

所以也许我可以在 Configuration() 中手动调用 Seed() 并进行一些检查以避免添加重复数据或修改已经存在的数据。

最佳答案

我最终得到了这个 Configuration 类:

public class Configuration : DbMigrationsConfiguration<Context.MyDBContext>
{
private readonly bool _pendingMigrations;

public Configuration()
{
AutomaticMigrationsEnabled = true;

// Check if there are migrations pending to run, this can happen if database doesn't exists or if there was any
// change in the schema
var migrator = new DbMigrator(this);
_pendingMigrations = migrator.GetPendingMigrations().Any();

// If there are pending migrations run migrator.Update() to create/update the database then run the Seed() method to populate
// the data if necessary
if (_pendingMigrations)
{
migrator.Update();
Seed(new Context.MyDBContext());
}
}

protected override void Seed(Context.MyDBContext context)
{
// Microsoft comment says "This method will be called after migrating to the latest version."
// However my testing shows that it is called every time the software starts

// Run whatever you like here

// Apply changes to database
context.SaveChanges();
base.Seed(context);
}
}

所以在这种情况下,Seed() 方法会在创建数据库时以及存在挂起的迁移时被调用。

这是我的 MyDBContext 类:

public class MyDBContext: DbContext
{
public MyDBContext() : base(AppSettings.DBConnectionString)
{
}

public static MyDBContext Create()
{
return new MyDBContext();
}

public DbSet<User> Users { get; set; }
public DbSet<Entries> Entries { get; set; }
}

关于c# - MigrateDatabaseToLatestVersion 没有运行 Seed() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36024400/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com