Hi Jakob,
The Attach/AttachAll method is usually used in the scenario where the entity
objects are serialized to client over a network and detached from their data
context and the clients require to update or delete data.
Note that only call the Attach methods on new or deserialized entities. The
only way for an entity to be detached from its original data context is for
it to be serialized. If you try to attach an undetached entity to a new data
context, and that entity still has deferred loaders from its previous data
context, LINQ to SQL will thrown an exception.
For more information on how to use the Attach/AttachAll method, please refer
to the following MSDN document:
'Data Retrieval and CUD Operations in N-Tier Applications (LINQ to SQL)'
http://msdn2.microsoft.com/en-us/library/bb546187.aspx As for your original question of how to cancel current editing, LINQ to SQL
doesn't provide such a function so far. A workaround is to implement the
IEditableObject interface on the entity class. The following is a sample:
public class Customer : IEditableObject
{
struct CustomerData
{
internal string id;
internal string firstName;
internal string lastName;
}
private CustomerData custData;
private CustomerData backupData;
private bool inTxn = false;
// Implements IEditableObject
void IEditableObject.BeginEdit()
{
Console.WriteLine("Start BeginEdit");
if (!inTxn)
{
this.backupData = custData;
inTxn = true;
Console.WriteLine("BeginEdit - " +
this.backupData.lastName);
}
Console.WriteLine("End BeginEdit");
}
void IEditableObject.CancelEdit()
{
Console.WriteLine("Start CancelEdit");
if (inTxn)
{
this.custData = backupData;
inTxn = false;
Console.WriteLine("CancelEdit - " + this.custData.lastName);
}
Console.WriteLine("End CancelEdit");
}
void IEditableObject.EndEdit()
{
Console.WriteLine("Start EndEdit" + this.custData.id +
this.custData.lastName);
if (inTxn)
{
backupData = new CustomerData();
inTxn = false;
Console.WriteLine("Done EndEdit - " + this.custData.id +
this.custData.lastName);
}
Console.WriteLine("End EndEdit");
}
public Customer(string ID)
: base()
{
this.custData = new CustomerData();
this.custData.id = ID;
this.custData.firstName = "";
this.custData.lastName = "";
}
public string ID
{
get
{
return this.custData.id;
}
}
public string FirstName
{
get
{
return this.custData.firstName;
}
set
{
this.custData.firstName = value;
}
}
public string LastName
{
get
{
return this.custData.lastName;
}
set
{
this.custData.lastName = value;
}
}
}
Suppose that you bind the LINQ query result to a BindingSource named "bs",
then you can call the CancelCurrentEdit method to cancel the current editing
as follows:
this.BindingContext[bs].CancelCurrentEdit();
Your other questions:
[quoted text, click to view] > If I use LINQ-to-SQL and bind resulting entity classes/collections to my
> forms in controls like DataGridView, how should I send the updates back?
Call the SubmitChanges method on the DataContext object.
[quoted text, click to view] > With datasets I instantiate a dataset adapter and call its Update method
> with the dataset which means all will be sorted out. The updates are all
> flagged within the dataset. But in LINQ-to-SQL I guess it is the
> datacontext that keeps track of the updates?
Yes, it is.
[quoted text, click to view] > I would feel a bit more safe if I could find a mechanism to just save one
> kind of objects at the time. Second best would be if I could clear
> specified types from the ChangeSet before I saved.
No, what you want is not available so far.
When you make the call of SubmitChanges, the DataContext tries to translate
your changes into equivalent SQL commands. You can use your own custom logic
to override these actions, but the order of submission is orchestrated by a
service of the DataContext known as the change processor.
Hope this helps.
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.
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notifications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx. ==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.