【问题标题】:SQL Server CE. Delete data from all tables for integration testsSQL 服务器 CE。从所有表中删除数据以进行集成测试
【发布时间】:2013-03-18 22:00:29
【问题描述】:

我们使用 SQL Server CE 进行集成测试。在每次测试之前的那一刻,我们从所有列中删除所有数据,然后重新播种测试数据。当结构发生变化时,我们会删除数据库文件。

为了删除数据,我们需要以正确的顺序遍历每个表并发出Delete from table blah,这很容易出错。很多时候,我只是在添加新实体时忘记添加删除语句。因此,如果我们可以自动从表中删除数据,那就太好了。

我看到了 Jimmy Bogard 对deletion of data in the correct order 的好意。我已经为实体框架实现了这一点,并且可以在成熟的 SQL Server 中使用。但是当我尝试在 SQL CE 中使用它进行测试时,我得到了异常,说

System.Data.SqlServerCe.SqlCeException : The specified table does not exist. [ @@sys.tables ]

SQL CE 没有包含所需信息的支持系统表。

是否有适用于 SQL CE 版本的脚本,可以删除所有表中的所有数据?

【问题讨论】:

    标签: entity-framework sql-server-ce integration-testing


    【解决方案1】:

    SQL Server Compact 实际上有列出所有表的系统表。在我的 SQL Server Compact 脚本 API 中,我有代码以“正确”的顺序列出表,这不是一项简单的任务!我使用 QuickGraph,它有一个用于对 DataSet 进行排序的扩展方法。您应该能够在测试代码中重用其中的一些: 33

    public void SortTables()
    {
        var _tableNames = _repository.GetAllTableNames();
        try
        {
            var sortedTables = new List<string>();
            var g = FillSchemaDataSet(_tableNames).ToGraph();
            foreach (var table in g.TopologicalSort())
            {
                sortedTables.Add(table.TableName);
            }
            _tableNames = sortedTables;
            //Now iterate _tableNames and issue DELETE statement for each
        }
        catch (QuickGraph.NonAcyclicGraphException)
        {
            _sbScript.AppendLine("-- Warning - circular reference preventing proper sorting of tables");
        }
    }
    

    您必须添加 QuickGraph DLL 文件(来自 Codeplex 或 NuGet),您可以在此处找到 GetAllTableNames 和 FillSchemaDataSet 的实现 http://exportsqlce.codeplex.com/SourceControl/list/changesets(在 Generator.cs 和 DbRepository.cs 中)

    【讨论】:

    • 哦,哇!看起来很有趣。我在寻找答案的时候看过你的项目,但没有多加注意。我一定会继续努力的!
    • 如果我能提供帮助,请告诉我!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-16
    • 1970-01-01
    • 2018-06-11
    • 1970-01-01
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多