Problems with LINQ to SQL

I am having problems with LINQ to SQL objects:

// Context is DataContext that was auto genereted when i create my .dbml file var cl = Context.Classes.ToArray(); var rm = Context.Rooms.ToArray(); List<DaySchedule> s = new List<DaySchedule>(); s.Add(new DaySchedule() { Class = cl[0], DayOfWeek = 0, Pair = 1, Room = rm[0] }); Context.SubmitChanges(); 

so after "SubmitChanges" the new DaySchedules will be saved in db. BUT I did not call the InsertOnSubmit function, and I do not want to save this DaySchedule.

BTW, if I use the following code:

 s.Add(new Acceron.University.DBAccess.DaySchedule() { Class_id = cl[0].Class_ID, DayOfWeek = 0, Pair = 1, Room_id = rm[0].Room_ID }); 

It will not be automatically saved in db.

Could you explain that this is a bug or function and how can I solve it?

+4
source share
1 answer

This is by design. The class and room are objects of context, as they were requested against the context. At any time, when an object with a context support adds children, it automatically transfers these changes to the context and marks it as inserted. Thus, you cannot add new objects without the auto-queue function. I highly recommend not causing save changes later.

+4
source

All Articles