Roman Golenok
Roman Golenok

Reputation: 1417

LINQ to SQL trouble

i have following trouble with LINQ to SQL entities:

// 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" new DaySchedules will be saved to db. BUT i didn't call InsertOnSubmit function and i don't want to save this DaySchedule.

BTW, if i will using 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 auto saved to db.

Could you explain is it bug or feature and how i can solve it?

Upvotes: 4

Views: 172

Answers (1)

Brian Mains
Brian Mains

Reputation: 50728

It is by design. Class and Room are context-aware entities, since they were queried against the context. Anytime a context-aware entity adds children, it queues up those changes automatically to the context and marks it as inserted. So you cannot add new entities without the auto-queuing feature. I'd highly recommend not calling save changes later on.

Upvotes: 4

Related Questions