问题描述
今天,当我尝试导入多个数据记录时,我注意到导入服务中有一个奇怪的行为.
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.
这篇关于InsertAllOnSubmit 只插入第一条数据记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!