Hello I have a table that has an “id” field and a “parentid” field. Both fields are required so you can’t insert null into a parentid column to mark it as a “root” to the hierarchy. I’m having an issue with linq where when I create a new “root” I have to put in a dummy parentid until I know the id of the new root, then I have to updated the parentid field with the correct id and save it again. This seems silly, but I haven’t found a better way to accomplish this yet?
TableWithHeirachy xobj = new TableWithHeirachy();
xobj.property1 = "test";
db.TableWithHeirachy.InsertOnSubmit(xobj);
db.SubmitChanges();
xobj.parentid = xobj.id;
db.SubmitChanges();
This seems really bad. Please tell me there is a better way!
A SQL Statement won’t know the future ID of the row it’s about to insert, until the insert has completed.
So, either continue as you are now – or instead use special values to indicate root records (null, -1, etc).