问题描述
我正在开发一个使用 EFCore 2.1.0-preview1-final 代码优先方法的项目。像在EF6(和以前的版本)中一样,我要确保DbContext(和模型)与数据库的兼容性。
I'm working on a project that uses EFCore 2.1.0-preview1-final code first approach. Like in EF6 (and previous versions) I want to ensure the compatibility of my DbContext (and models) to the database.
在EF6中,默认情况下启用此功能可以使用。据我所知,EF使用 __ MigrationHistory
表存储模型信息。 EFCore在 __ EFMigrationsHistory
表中没有可以提供此类信息的列。
In EF6 it was enabled by default and it was possible to deactivate it with Database.CompatibleWithModel(false);
. As far as I know EF uses the __MigrationHistory
table where the model information was stored. EFCore has no such column in __EFMigrationsHistory
table that could provide such information.
我找不到有关兼容性检查的任何信息在EFCore中。但我想确保兼容性,因为经过一些测试,它似乎默认情况下未启用(或存在)。我通过手动添加和删除数据库中的某些列并在修改后执行应用程序进行了测试。我-出乎我的意料-没有例外。
I cannot find any information about compatibility check in EFCore. But I want to ensure the compatibility, because after some tests it seems not to be enabled by default (or does exist). I tested it by adding and deleting some columns from database manually and executing the application after the modifications. I - against my expectation - received no exception.
有人像EF6 for EFCore一样知道如何实现模型到数据库的兼容性检查吗?
还是可以提供一些有用的链接以获取有关它的更多信息,或者为什么它不存在于EFCore中(因为不必要)?
Does anybody know how to achieve a compatibility check from model to database and vice versa like in EF6 for EFCore?Or could provide some helpful links for further information about it or why it doesn't exist in EFCore (because it is not necessary)?
推荐答案
我 强烈 建议不要这样做,因为它使用内部组件并且容易出错,但这是实现此目的的一种方法。
I strongly advise against doing this since it uses internal components and is error-prone, but here's one way to do it.
using (var db = new MyDbContext())
{
var reporter = new OperationReporter(handler: null);
var designTimeServiceCollection = new ServiceCollection()
.AddSingleton<IOperationReporter>(reporter)
.AddScaffolding(reporter);
new SqlServerDesignTimeServices().ConfigureDesignTimeServices(designTimeServiceCollection);
var designTimeServices = designTimeServiceCollection.BuildServiceProvider();
var databaseModelFactory = designTimeServices.GetService<IScaffoldingModelFactory>();
var databaseModel = (Model)databaseModelFactory.Create(
db.Database.GetDbConnection().ConnectionString,
tables: new string[0],
schemas: new string[0],
useDatabaseNames: false);
var currentModel = db.Model;
// Fix up the database model. It was never intended to be used like this. ;-)
foreach (var entityType in databaseModel.GetEntityTypes())
{
if (entityType.Relational().Schema == databaseModel.Relational().DefaultSchema)
{
entityType.Relational().Schema = null;
}
}
databaseModel.Relational().DefaultSchema = null;
databaseModel.SqlServer().ValueGenerationStrategy =
currentModel.SqlServer().ValueGenerationStrategy;
// TODO: ...more fix up as needed
var differ = db.GetService<IMigrationsModelDiffer>();
if (differ.HasDifferences(databaseModel, currentModel))
{
throw new Exception("The database and model are out-of-sync!");
}
}
这篇关于EF Core中是否存在与Database.CompatibleWithModel(bool)等效的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!