如何使用ServiceStack.OrmLite从现有数据中获取实体列表?

时间:2019-05-16 12:41:28

标签: servicestack ormlite-servicestack tooling

我正在使用测试数据来运行集成测试。所以我正在使用以下方法

public static IEnumerable<ProductPhase> GetProductPhases()
{
    return new List<ProductPhase>
    {
        new ProductPhase { Id = "FirstPhase", Order = 1 },
        new ProductPhase { Id = "SecondPhase", Order = 2 },
        new ProductPhase { Id = "ThirdPhase", Order = 3 }, // ...
    }
}

[Test]
public void MyTest()
{
    using (var db = HostContext.TryResolve<IDbConnectionFactory>.OpenDbConnection())
    {
        db.DeleteAll<ProductPhase>();
        db.InsertAll(GetProductPhases());
    }

    // Do test stuff ...
}

要完全验证集成测试,我需要与生产中插入的数据同步。该数据可能包含数十行,并且每次我需要手动重构GetProductPhases()方法时,我就接近 reality

我正在寻找的是一种选择现有SQL数据的方法,该方法可以将粘贴复制到ProductPhase对象初始化程序中。

因此,可以调用以下内容:

db.Select<ProductPhase>().DumpList() // where db is connected to production server, so I can get actual values and it returns a string with new List<ProductPhase> { new ProductPhase { Id = "FirstPhase", Order = 1 }, new ProductPhase { Id = "SecondPhase", Order = 2 }, new ProductPhase { Id = "ThirdPhase", Order = 3 }, // ... }

也许有更好的方法可以做到这一点,我不知道,我愿意接受替代方案。

谢谢。

1 个答案:

答案 0 :(得分:1)

我将使用.ToCsv()扩展名方法将结果保存到CSV,而不是在测试开始之前将其反序列化。通常,我会将CSV保存在外部.csv文件中(该文件已设置为复制到输出文件夹),因此易于更新。