InsertAllOnSubmit仅插入第一个数据记录

时间:2022-07-26 16:20:56

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();

Same here:

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.

有人知道为什么会这样吗?根据我的理解,所有三种变体都应该导入所有数据记录,但缺少的自动递增值表明它不是那种方式。

[EDIT] Added two screenshots.

[编辑]添加了两个截图。

5 个解决方案

#1


6  

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.

我遇到了同样的问题,事实证明问题是由于覆盖了映射类的Equals。 My Equals方法仅比较作为标识字段的主键字段。当然,当对象是新的时,所有的身份都是0.所以当调用InsertAllOnSubmit时,它认为所有新对象都是相同的,并且除了第一个之外基本上都忽略了每个对象。

#2


2  

Not quite sure why the 2nd variation doesn't work, however, shouldn't the last one be:

不太确定为什么第二个变体不起作用,但最后一个不应该是:

dbc.Property.InsertallOnSubmit(properties);
dbc.SubmitChanges();

Edit

For the second loop try:

对于第二个循环尝试:

foreach (Property prop in properties) 
{   
    var newProp = new Property();
    newProp = prop;
    dbc.Property.InsertOnSubmit(newProp);
}
dbc.SubmitChanges();

For the last solution try:

对于最后的解决方案尝试:

dbc.Property.InsertAllOnSubmit(properties.ToList());
dbc.SubmitChanges();

#3


1  

As other users are having the same strange behaviour, I've reported the issue as a bug to Microsoft:

由于其他用户具有相同的奇怪行为,我已将此问题报告为Microsoft的错误:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483711

#4


1  

I encountered this problem a few minutes ago.

我几分钟前遇到过这个问题。

My issue was that the list I sent to InsertAllOnSubmit<mappedClass>() was full of objects that originated from a single instance of mappedClass. I was modifying the members depending on the instance of the view model I wanted to add to the database and then re-adding the instance to the list.

我的问题是我发送给InsertAllOnSubmit ()的列表中充满了源自mappedClass的单​​个实例的对象。我正在修改成员,具体取决于我想要添加到数据库的视图模型的实例,然后将实例重新添加到列表中。

Seems like a newbie mistake but it could be something to check on if anyone is still having this problem!

看起来像是一个新手的错误,但如果有人仍然遇到这个问题,可能需要检查一下!

#5


0  

Just use this one, perfect, solution.
As, We have a new entity like "TestTable".
Initialize this entity inside the for loop as

只需使用这个完美的解决方案。因为,我们有一个像“TestTable”这样的新实体。在for循环中初始化此实体为

TestTable objTable = new TestTable ();

And add the entity items as well as in list object of List<TestTable> in for loop.
And move InsertAllOnSubmit() outside the for loop and now it'll must work.

并在for循环中添加实体项以及List 的列表对象。并在for循环外移动InsertAllOnSubmit(),现在它必须工作。

#1


6  

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.

我遇到了同样的问题,事实证明问题是由于覆盖了映射类的Equals。 My Equals方法仅比较作为标识字段的主键字段。当然,当对象是新的时,所有的身份都是0.所以当调用InsertAllOnSubmit时,它认为所有新对象都是相同的,并且除了第一个之外基本上都忽略了每个对象。

#2


2  

Not quite sure why the 2nd variation doesn't work, however, shouldn't the last one be:

不太确定为什么第二个变体不起作用,但最后一个不应该是:

dbc.Property.InsertallOnSubmit(properties);
dbc.SubmitChanges();

Edit

For the second loop try:

对于第二个循环尝试:

foreach (Property prop in properties) 
{   
    var newProp = new Property();
    newProp = prop;
    dbc.Property.InsertOnSubmit(newProp);
}
dbc.SubmitChanges();

For the last solution try:

对于最后的解决方案尝试:

dbc.Property.InsertAllOnSubmit(properties.ToList());
dbc.SubmitChanges();

#3


1  

As other users are having the same strange behaviour, I've reported the issue as a bug to Microsoft:

由于其他用户具有相同的奇怪行为,我已将此问题报告为Microsoft的错误:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=483711

#4


1  

I encountered this problem a few minutes ago.

我几分钟前遇到过这个问题。

My issue was that the list I sent to InsertAllOnSubmit<mappedClass>() was full of objects that originated from a single instance of mappedClass. I was modifying the members depending on the instance of the view model I wanted to add to the database and then re-adding the instance to the list.

我的问题是我发送给InsertAllOnSubmit ()的列表中充满了源自mappedClass的单​​个实例的对象。我正在修改成员,具体取决于我想要添加到数据库的视图模型的实例,然后将实例重新添加到列表中。

Seems like a newbie mistake but it could be something to check on if anyone is still having this problem!

看起来像是一个新手的错误,但如果有人仍然遇到这个问题,可能需要检查一下!

#5


0  

Just use this one, perfect, solution.
As, We have a new entity like "TestTable".
Initialize this entity inside the for loop as

只需使用这个完美的解决方案。因为,我们有一个像“TestTable”这样的新实体。在for循环中初始化此实体为

TestTable objTable = new TestTable ();

And add the entity items as well as in list object of List<TestTable> in for loop.
And move InsertAllOnSubmit() outside the for loop and now it'll must work.

并在for循环中添加实体项以及List 的列表对象。并在for循环外移动InsertAllOnSubmit(),现在它必须工作。