The following code loads a gig, clears out the gigs acts collection and then adds a new act.
Data.LinqToSQL.Gig dbGig = DBContext.Gigs.Where(x => x.ID == myGigID).SingleOrDefault();
//Remove all references to the current acts
if(dbGig.Acts!=null) {
DBContext.Acts.DeleteAllOnSubmit(dbGig.Acts);
}
Data.LinqToSQL.Act dbAct = new ListenTo.Data.LinqToSQL.Act();
dbAct.ID = Guid.NewGuid();
DBContext.Acts.InsertOnSubmit(dbAct);
DBContext.SubmitChanges();
Please note that everytime I run this code, the value of myGigID is the same, so its always the same gig that I load.
The first time I run this code it works fine and I have a gig with 1 act.
The second time, dbGig.Acts (a collection) has a count of 0 and so the DeleteAllOnSubmit does not remove any acts. However in the database there is 1 act for this gig! Hence I end up with 2 acts once this code has run.
If I run it a 3rd time, I end up with 3 acts in total.
Any ideas what I'm doing wrong?