views:

1235

answers:

4

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.

+1  A: 

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();
James
Michael Barth
Do you get any errors?
James
No errors. I've added two screenshots to my original post. As you can see, with the first method all properties get IDs (the numbers in squared brackets []). On the second screenshot, only the first property gets an ID. I have absolutely no idea what's wrong since the provided code is the only thing that's different in both screenshots!
Michael Barth
Possible bug in the LINQ code?
James
Maybe that's it... I couldn't find anything else that's wrong.
Michael Barth
Try doing "InsertAllOnSubmit(properties.ToList())"
James
I'll try it this monday :)
Michael Barth
Same as before for both variants, only the first property gets an ID. Anyway, thank you for your continuing interest to help.
Michael Barth
Hmm very odd, only thing I can think of then is it is a bug with the LINQ code. I will try a small test app and get back to you.
James
A: 

I am having the same problem. I wonder if it is related to .Net3.5 SP1 because I'm sure this was working before. But I'm not sure the problem is precisely correlated with the SP1 upgrade.

Bob W
Then that's two of us using .NET 3.5 with SP1 where it doesn't work, maybe three: http://stackoverflow.com/questions/1171111/linq-to-sql-data-context-doesnt-commit-if-multiple-inserts-are-called-on-table-wi
Michael Barth
A: 

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

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

Michael Barth
Alas, I could not reproduce the bug at home and the ticket was closed as the Linq team couldn't reproduce it either. :/
Michael Barth
+1  A: 

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.

Nicholas Roeder