Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
508 views
in Technique[技术] by (71.8m points)

c# - InsertAllOnSubmit only inserts first data record

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...