Linq to SQL Data context does not commit if multiple auto-increment entries are called in a table

I have a problem when trying to send two inserts at once. The table has a primary key with automatic increment. Installed comment objects do not have an identifier set so that the database can assign it.

My code works for single inserts, if I send right away, but if I try to execute several InsertOnSubmit commands, then it does not seem to matter and does not return any errors or exceptions, even if my code is in a try and catch block. Has anyone else had this problem or do you know which direction I should look?

Example 1 (this does not commit the database)

//myComment is initialised with data
dc.tblDailyComments.InsertOnSubmit(myComment);


//myComment2 is initialised with different data
dc.tblDailyComments.InsertOnSubmit(myComment2); 


//when this is called it does not commit to the database   
dc.SubmitChanges();

Example 2 (this works great)

//myComment is initialised with data
dc.tblDailyComments.InsertOnSubmit(myComment);

//commits to the database   
dc.SubmitChanges();

//myComment2 is initialised with different data
dc.tblDailyComments.InsertOnSubmit(myComment2); 


//commits to the database   
dc.SubmitChanges();
+3

Source: https://habr.com/ru/post/1713331/


All Articles