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.
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.
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();
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
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.
Seems like a newbie mistake but it could be something to check on if anyone is still having this problem!
Just use this one, perfect, solution.
As, We have a new entity like "TestTable".
Initialize this entity inside the for loop as
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.