v-lliu
6/5/2008 11:57:00 AM
Thanks Patrice and Cor for your help!
Hi Jakob,
Thank you for your prompt response!
I do more research on this issue and find that implementing the
IEditableObject inteface on the entity class is not practical to your
scenario. You're right that only when the edits happen in UI, the methods
of the IEditableObject interface are called.
As Patrice as suggested, we can make use of the GetChangeSet method of the
DataContext object to get the changes that the DataContext tracks. However
this is not enough, because the return value of the GetChangeSet method
only contains current updated values of the items.
A workaround is to add a public property for each public property in the
entity class to maintain the previous value. We can set the initial value
of the property in the OnLoaded method and update it int the On**Changing
method.
For example, let's assume that we have a table named "People" in DB and the
table has two columns:
ID int primary
Name varchar(50)
Create a WinForm Application project in VS2008 and add a "LINQ to SQL
Classes" in the project. From the Server Explorer, add a connection to the
DB and drag the table "People" onto the left panel of the DataClass's
designer.
Add a class file in the project and paste the following code to the file:
partial class People
{
public int OrigionID { get; set; }
public string OrigionName { get; set; }
partial void OnIDChanging(int value)
{
OrigionID = this._ID;
}
partial void OnNameChanging(string value)
{
OrigionName = this._Name;
}
partial void OnCreated()
{
OrigionID = _ID;
OrigionName = _Name;
}
partial void OnLoaded()
{
OrigionID = _ID;
OrigionName = _Name;
}
}
Add two Buttons on the Form1 and paste the following code in the Form1.cs:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataClasses1DataContext dc = new DataClasses1DataContext();
BindingSource bs = new BindingSource();
private void Form1_Load(object sender, EventArgs e)
{
var q = from p in dc.Peoples
select p;
bs.DataSource = q;
}
// change values
private void button1_Click(object sender, EventArgs e)
{
IList list = bs.List;
(list[0] as People).Name = "aa";
}
// get changes
private void button2_Click(object sender, EventArgs e)
{
ChangeSet cs = dc.GetChangeSet();
IList<object> updates = cs.Updates;
foreach (object obj in updates)
{
Console.WriteLine("Origion_ID=" + (obj as
People).OrigionID.ToString() + " Origion_name=" + (obj as
People).OrigionName);
}
}
}
Hope this is what you want.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
msdnmg@microsoft.com.
This posting is provided "AS IS" with no warranties, and confers no rights.