EF Core 中是否存在等效于 Database.CompatibleWithModel(bool) [英] Does an equivalent to Database.CompatibleWithModel(bool) exist in EF Core

查看:29
本文介绍了EF Core 中是否存在等效于 Database.CompatibleWithModel(bool)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用 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 中它默认启用,可以使用 Database.CompatibleWithModel(false);.据我所知,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 的 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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆