Entity enhancements

Watch the Entity Enhancements video on YouTube.

Detach

Being unable to detach entities is the most common complaint about LINQ to SQL. PLINQO makes it easy to work with an entity independently of a DataContext, then attach it to another one when you're ready to save your changes:

Task task = null;
using (var context = new TrackerDataContext())
{
    task = context.Task.FirstOrDefault(t => t.Id == 1);
    task.Detach();
}

task.StatusId = 1;

using (var context2 = new TrackerDataContext())
{
    context2.Task.Attach(task, true);
    context2.SubmitChanges();
}

This code works as is if your database supports row versioning. If it doesn't, set the Update Check policy of each column in the dbml file to Never for detach to work.

Clone

PLINQO uses the WCF DataContractSerializer to clone entities. Call the Clone method on any entity PLINQO generates to get a copy in its own memory location. In this example, a user is retrieved from the database and cloned, one property is changed, and the clone is saved as a new user:

using (var context = new TrackerDataContext())
{
    var u = context.Manager.User.GetByKey(1);
    User clonedUser = u.Clone();
    clonedUser.Id = 0;
    context.User.InsertOnSubmit(clonedUser);
    context.SubmitChanges();
}

Serialization

Serializing LINQ to SQL entities has long frustrated developers. PLINQO removes the frustration by eliminating circular references and using WCF data contract serialization:

Task task = context.Task.GetByKey(1);
string xml = task.ToXml();
byte[] b = task.ToBinary();

Deserializing is just as simple:

task = Task.FromXml(xml);
task = Task.FromBinary(b);