InsertAllOnSubmit 只插入第一条数据记录

InsertAllOnSubmit only inserts first data record(InsertAllOnSubmit 只插入第一条数据记录)
本文介绍了InsertAllOnSubmit 只插入第一条数据记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

今天,当我尝试导入多个数据记录时,我注意到导入服务中有一个奇怪的行为.

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 只插入第一条数据记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Is Unpivot (Not Pivot) functionality available in Linq to SQL? How?(Linq to SQL 中是否提供 Unpivot(非 Pivot)功能?如何?)
How to know if a field is numeric in Linq To SQL(如何在 Linq To SQL 中知道字段是否为数字)
Linq2SQl eager load with multiple DataLoadOptions(具有多个 DataLoadOptions 的 Linq2SQl 急切加载)
Extract sql query from LINQ expressions(从 LINQ 表达式中提取 sql 查询)
LINQ Where in collection clause(LINQ Where in collection 子句)
Orderby() not ordering numbers correctly c#(Orderby() 没有正确排序数字 c#)