有没有办法在Entity Framework Core中以编程方式检查未决模型更改?

亚瑟·海特

目前,我正在为ASP.NET Core WebAPI开发设置团队环境,结合使用xUnit进行单元测试以及GitLab CI。对于数据库通信,我们使用EF Core。

对于EF Core,我们将使用代码优先迁移,我们担心开发人员可能只会更新模型,而不会为模型更改创建迁移。因此,我们希望CI运行代码库中存在的所有迁移,将它们与代码优先模型的当前状态进行比较,并在代码优先模型状态与运行所有迁移所导致的状态不相等时失败。

有没有办法做到这一点?我在EF Core文档中找不到关于此的任何信息。

亚瑟·海特

感谢@ErikEJ的示例代码,我能够编写出完全符合我想要的以下测试:

    using FluentAssertions;
    using Microsoft.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore.Infrastructure;
    using Microsoft.EntityFrameworkCore.Migrations;
    using Xunit;

    /// <summary>
    /// Contains a test that verifies that the
    /// model does not contain any changes that are not included
    /// in the migrations.
    /// </summary>
    public class NoPendingModelChangesTest
    {
        private static readonly string DummyConnectionString = @"Server=localhost;Database=DoesNotExist;Trusted_Connection=True;";

        /// <summary>
        /// Tests that the current model does not contain any changes
        /// that are not contained in the database migrators.
        /// In other words: tests that the current model state equals the
        /// state that results from all the migrations combined.
        /// </summary>
        [Fact]
        public void ModelDoesNotContainPendingChanges()
        {
            // Do not use the test database, the SQL Server model provider must be
            // used as that is the model provider that is used for scaffolding migrations.
            using var ctx = new MyDatabase(
                new DbContextOptionsBuilder<MyDatabase>()
                    .UseSqlServer(DummyConnectionString)
                    .Options);

            var modelDiffer = ctx.GetService<IMigrationsModelDiffer>();
            var migrationsAssembly = ctx.GetService<IMigrationsAssembly>();

            var pendingModelChanges = modelDiffer
                .GetDifferences(
                    migrationsAssembly.ModelSnapshot?.Model,
                    ctx.Model);

            pendingModelChanges
                .Should()
                .BeEmpty(
                    because:
                        "the current model state should be equal to the state that results from all the migrations combined (try scaffolding a migration)");
        }
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

有没有办法在.NET Core库中包含.NET Framework代码?

来自分类Dev

有没有办法等待将来完成而不会阻塞Act Framework中的执行线程?

来自分类Dev

有没有办法在Zend Framework 2中将Firebird / Ibase与Doctrine结合使用?

来自分类Dev

有没有办法以编程方式添加用户?

来自分类Dev

有没有办法从 Linq to Entity 查询中的日期获取长值?

来自分类Dev

有没有办法在React Native中以编程方式更改SegmentedControlIOS选定的选项?

来自分类Dev

有没有办法在Android中以编程方式更改子字符串的颜色?

来自分类Dev

有没有办法检查WebDav中的凭据?

来自分类Dev

有没有办法在leveldb中更改键?

来自分类Dev

有没有办法“杀死”模型财产?

来自分类Dev

有没有办法自动对模型应用限制?

来自分类Dev

有没有办法检查$ _POST是否存在?

来自分类Dev

有没有办法使用Firebase检查凭证?

来自分类Dev

有没有办法检查谁执行查询?

来自分类Dev

有没有办法从ListView项目更改屏幕?

来自分类Dev

有没有办法更改UILocalNotification的前文本?

来自分类Dev

有没有办法更改BIOS引导屏幕?

来自分类常见问题

有没有办法在PHP中扩展特征?

来自分类Dev

有没有办法清除JavaScript中的对象?

来自分类Dev

有没有办法重置Angular中的$ touched?

来自分类Dev

有没有办法使ghc中的并置过载?

来自分类Dev

有没有办法刷新Inkscape中的扩展

来自分类Dev

有没有办法在POJO中访问SlingRepository?

来自分类Dev

有没有办法在Haskell中取消映射?

来自分类Dev

有没有办法在VBA中编辑公式

来自分类Dev

有没有办法限制Firebase中的注册

来自分类Dev

有没有办法在Spark中洗牌集合

来自分类Dev

有没有办法在MATLAB中增加“ realmax”?

来自分类Dev

有没有办法在cuBLAS中执行“ saypx”?

Related 相关文章

  1. 1

    有没有办法在.NET Core库中包含.NET Framework代码?

  2. 2

    有没有办法等待将来完成而不会阻塞Act Framework中的执行线程?

  3. 3

    有没有办法在Zend Framework 2中将Firebird / Ibase与Doctrine结合使用?

  4. 4

    有没有办法以编程方式添加用户?

  5. 5

    有没有办法从 Linq to Entity 查询中的日期获取长值?

  6. 6

    有没有办法在React Native中以编程方式更改SegmentedControlIOS选定的选项?

  7. 7

    有没有办法在Android中以编程方式更改子字符串的颜色?

  8. 8

    有没有办法检查WebDav中的凭据?

  9. 9

    有没有办法在leveldb中更改键?

  10. 10

    有没有办法“杀死”模型财产?

  11. 11

    有没有办法自动对模型应用限制?

  12. 12

    有没有办法检查$ _POST是否存在?

  13. 13

    有没有办法使用Firebase检查凭证?

  14. 14

    有没有办法检查谁执行查询?

  15. 15

    有没有办法从ListView项目更改屏幕?

  16. 16

    有没有办法更改UILocalNotification的前文本?

  17. 17

    有没有办法更改BIOS引导屏幕?

  18. 18

    有没有办法在PHP中扩展特征?

  19. 19

    有没有办法清除JavaScript中的对象?

  20. 20

    有没有办法重置Angular中的$ touched?

  21. 21

    有没有办法使ghc中的并置过载?

  22. 22

    有没有办法刷新Inkscape中的扩展

  23. 23

    有没有办法在POJO中访问SlingRepository?

  24. 24

    有没有办法在Haskell中取消映射?

  25. 25

    有没有办法在VBA中编辑公式

  26. 26

    有没有办法限制Firebase中的注册

  27. 27

    有没有办法在Spark中洗牌集合

  28. 28

    有没有办法在MATLAB中增加“ realmax”?

  29. 29

    有没有办法在cuBLAS中执行“ saypx”?

热门标签

归档