views:

52

answers:

3

For example

a = datacontext.customers.FirstOrDefaul();
b = datacontext.customers.Skip(1).FirstOrDefaul();

a.name="name1";
b.Name="name2";

When i call datacontext.SubmitChanges(), Two object updated. I don't want this.

I need only Update a object. How do it?

EDIT

WPFWindow windowA=new WPFWINDOW()
windowA.DataContext=a;
windowA.Show();

WPFWindow windowB=new WPFWINDOW()
windowB.DataContext=b;
windowB.Show();

When clicking save button from window, corresponding object saving.
When clicking cancel button, corresponding object not saved

+1  A: 

You need to use separate DataContext's.

leppie
A: 

if you don't want to save changes to b, then why assign b.name = "name2". Use a temporary variable to store "name2". Later you can assign it to b.name if you want

Veer
not use temporary. User maybe save "b" object. maybe cancel "b" object.
ebattulga
A: 

My understanding is that the purpose of a DataContext is to encapsulate a set of changes to be submitted together. If you want some changes to be separate, create another DataContext instance. DataContexts, I believe, are intended to be lightweight and quick to create and release. In the application I have created that uses LINQ to SQL heavily, I create a separate DataContext for every window. Do NOT use a shared data context for your whole application. It's not like a database connection. That's why the database connection is a separate object.

BlueMonkMN