我在 Xamarin.iOS 应用程序中使用 Entity Framework Core。
在包含 iOS 应用程序和其他应用程序之间共享的代码 (.netstandard 2.0) 的核心项目中,我想知道是否需要迁移以便我也可以执行一些其他操作。
这里是上下文:
public void Initialize()
{
using (var dbContext = new MyDbContext(m_dbContextOptions))
{
--> bool isNeeded = demoTapeDbContext.Database.IsMigrationNeeded()
demoTapeDbContext.Database.Migrate();
}
}
我发现最接近的方法是调用方法 GetPendingMigrationsAsync()
并检查未决迁移的数量,但我不确定这是否是在 Entity Framework 中进行此类检查的最安全方法:
public async Task InitializeAsync()
{
using (var dbContext = new MyDbContext(m_dbContextOptions))
{
bool isMigrationNeeded = (await demoTapeDbContext.Database.GetPendingMigrationsAsync()).Any();
demoTapeDbContext.Database.Migrate();
}
}
最佳答案
您是正确的,GetPendingMigrationsAsync
方法是您应该使用的方法。来自 the docs :
Asynchronously gets all migrations that are defined in the assembly but haven't been applied to the target database.
如果你看the code ,您可以追踪它是如何工作的。 If 获取程序集中定义的所有迁移,并通过查询数据库删除它找到的迁移。
关于c# - 在 EF Core 中,如何检查是否需要迁移?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54356964/