i have two tables realted by 1 – 1 realtion. something like:
DOC: Id,SN (Id is primary key)
Sub: Id,Name (Id is foreign key)
DOC.Id and Sub.Id are related 1 – 1 as i mentioned
now in business objects i feel the data class as follow:
DOC doc = new DOC();
doc.SN = 1;
Sub sub = new Sub();
sub.Name = "name";
doc.Sub = sub;
using(dbDataContext db = new dbDataContext())
{
db.DOC.InsertOnSubmit(doc);
db.SubmitChanges();
sub.Id = doc.Id;
db.Sub.InsertOnSubmit(sub);
db.SubmitChanges();
}
the last SubmitChanges() throw an exception (can’t insert object is already existed)
back to the database i found out that both objects are inserted after executing that code.
is that an implicit transaction or am i doing something wrong
Linq2SQL is persisting the object graph on the first
SubmitChanges()i.e.
Saves
doc, and its childdoc.SubThis isn’t related to implicit transactions, it is just that when the Parent is attached to the datacontext with InsertOnSubmit(), L2SQL automagically attaches its children as well.
If you do want an explicit ‘insert’ for the Sub, then don’t assign the Sub to the Doc until after the Doc’s SubmitChanges() (i.e. Insert the doc with a null Sub).
It is quite difficult to manually detach an object from a DataContext once attached.