新建、删除和插入模式
3003 是确切的版本(我认为)。 我有下面的代码(使用 ActiveRecord)用于创建 2 个新实体,删除任何具有相同“Stamp1”和“Stamp2”值的实体,然后我想将所有内容插入我的 List
。
我无法判断代码对于删除是否有效,并且我不知道如何将列表中的所有新内容插入到批处理中。 在 v2.x 中,我使用了“SaveAll”,但我不明白 3 中的等效功能。
List<MyEntity> EntsList = new List<MyEntity>();
MyEntity myEntA = new MyEntity();
myEntA.Stamp1 = Convert.ToDateTime("1/1/1989");
myEntA.Stamp2 = Convert.ToDateTime("1/1/1990");
EntsList.Add(myEntA);
MyEntity myEntB = new MyEntity();
myEntB.Stamp1 = Convert.ToDateTime("1/1/1989");
myEntB.Stamp2 = Convert.ToDateTime("1/1/1990");
EntsList.Add(myEntB);
for(int d = 0; d < EntsList.Count; d++)
{
MyEntity delEnt =
MyEntity.SingleOrDefault (x => x.Stamp1 == EntsList[0].Stamp1 && x.Stamp2 == EntsList[0].Stamp2);
delEnt.Delete();
}
for(int d = 0; d < EntsList.Count; d++)
{
// How do I insert everything sequentially or all in one batch?
}
3003 is the exact version (I think). I have the below code (using ActiveRecord) for creating 2 new entities, deleting any that have the same "Stamp1" and "Stamp2" values, and then I want to insert everything in my List<MyEntity>
.
I cannot tell if the code is efficient for the delete, and I have no idea how to insert everything new from a list into a batch. With v2.x I used "SaveAll" but I don't understand the equivalent functionality in 3.
List<MyEntity> EntsList = new List<MyEntity>();
MyEntity myEntA = new MyEntity();
myEntA.Stamp1 = Convert.ToDateTime("1/1/1989");
myEntA.Stamp2 = Convert.ToDateTime("1/1/1990");
EntsList.Add(myEntA);
MyEntity myEntB = new MyEntity();
myEntB.Stamp1 = Convert.ToDateTime("1/1/1989");
myEntB.Stamp2 = Convert.ToDateTime("1/1/1990");
EntsList.Add(myEntB);
for(int d = 0; d < EntsList.Count; d++)
{
MyEntity delEnt =
MyEntity.SingleOrDefault (x => x.Stamp1 == EntsList[0].Stamp1 && x.Stamp2 == EntsList[0].Stamp2);
delEnt.Delete();
}
for(int d = 0; d < EntsList.Count; d++)
{
// How do I insert everything sequentially or all in one batch?
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用存储库的 Add 方法添加 IEnumerable 对象集合,这将在一个 BatchQuery 中添加所有对象。 所以你应该能够做类似的事情:
You can use the Add method of the repository to add an IEnumerable collection of objects and this will add all of your objects in one BatchQuery. So you should be able to do something like: