InsertAllOnSubmit 只插入第一条数据记录
- 作者: 丿樱花灬
- 来源: 51数据库
- 2022-12-13
问题描述
今天,当我尝试导入多个数据记录时,我注意到导入服务中有一个奇怪的行为.
I noticed a strange behaviour in my Import Service today when I tried to import multiple data records.
当我这样做时,所有数据记录都被导入并且自动递增的值是正确的(见截图):
When I do it like this, all data records are imported and the auto-incremented value is correct (see screenshot):
public void Create(List<Property> properties)
{
foreach (Property prop in properties) {
dbc.Property.InsertOnSubmit(prop);
dbc.SubmitChanges();
}
}
当我这样尝试时,只有第一个数据记录获得了正确的自动递增值(见截图):
When I try it like this, only the first data record get's a correct auto-incremented value (see screenshot):
foreach (Property prop in properties) {
dbc.Property.InsertOnSubmit(prop);
}
dbc.SubmitChanges();
这里也一样:
dbc.Property.InsertAllOnSubmit(properties); dbc.SubmitChanges();
有人知道为什么会这样吗?根据我的理解,所有三个变体都应该导入所有数据记录,但缺少自动递增的值表明情况并非如此.
Does anybody have an idea why it's like that? All three variants should import all data records according to my understanding, but the missing auto-incremented values indicate it's not that way.
添加了两个屏幕截图.
推荐答案
我遇到了同样的问题,结果证明问题是由于在映射类上覆盖了 Equals.我的 Equals 方法只是比较作为身份字段的主键字段.当然当对象是新的时,所有的标识都是0.所以当调用InsertAllOnSubmit时,它认为所有的新对象都是一样的,基本上忽略了第一个.
I had the same problem and it turned out the issue was due to overriding Equals on the mapped class. My Equals method was only comparing the primary key field which was an identity field. Of course when the objects are new, all identities are 0. So when InsertAllOnSubmit was called, it thought that all new objects were the same and basically ignored every one but the first.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
