Linq to SQL InsertOnSubmit for multiple objects

1.9k views Asked by At

I have a problem with Linq to SQL InsertOnSubmit, which only seems to work for the first item in a table.

For example with the following:

var noteDetail1 = new NoteDetail() { Title = "Test Note Title 1", NoteText = "Test note" };
var waiverDetail1 = new WaiverDetail() { Title = "Test Waiver Title 1", NoteText = "Test waiver details text" };
var riskDetail1 = new RiskDetail() { Title = "Test Risk Title 1", NoteText = "Test risk details text" };
context.Notes.InsertOnSubmit(noteDetail1);
context.Notes.InsertOnSubmit(riskDetail1);
context.Notes.InsertOnSubmit(waiverDetail1);
context.SubmitChanges();

I only get the first entity ("Test Note Title 1") inserted into the database. If I place a SubmitChanges after each InsertOnSubmit, all the rows are successfully inserted.

The above Types are all inherited from a Note class, so are inserted into the same table.

I am, however, experiencing the same problem with non-derived classes.

I've spent a long time looking at this but can't find what I've done wrong. The whole idea of InsertOnSubmit/SubmitChanges is so that you can do multiple changes so there must be something simple I am missing.

1

There are 1 answers

0
Darren On BEST ANSWER

The problem was that I had overriden Equals in my entity classes so that entities with the same Id were considered the same. Obviously, Linq to SQL is using this at some point and getting the result that all new entities are equal (because they all have the Id of 0).

Thanks Jonathan for being my "Rubber Duck".