如何使用 ServiceStack.OrmLite 从现有数据中获取实体列表?
How can I get a list of entities from existing data with ServiceStack.OrmLite?
我正在使用测试数据进行 运行 集成测试。所以我使用下面的方法
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.
我正在寻找的方法是将 select 现有 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 }, // ... }
也许有更好的方法,我不知道,我愿意接受其他方法。
谢谢。
我会使用 .ToCsv()
扩展方法将结果保存到 CSV,而不是在测试开始前将其反序列化。通常我会将 CSV 保存在外部 .csv
文件中(设置为复制到输出文件夹)以便于更新。
我正在使用测试数据进行 运行 集成测试。所以我使用下面的方法
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.
我正在寻找的方法是将 select 现有 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 }, // ... }
也许有更好的方法,我不知道,我愿意接受其他方法。
谢谢。
我会使用 .ToCsv()
扩展方法将结果保存到 CSV,而不是在测试开始前将其反序列化。通常我会将 CSV 保存在外部 .csv
文件中(设置为复制到输出文件夹)以便于更新。