Entity Enhancements

Entity Enhancements

If you want some more info about detaching, cloning, and serialization, take a look at this quick overview!

Detach

Inability to detach is what coders have complained about most when working with LINQ to SQL. Well, complain no more. PLINQO makes it easy to work with entities independent from a datacontext and attach to another when you are ready to save your changes. The following code is now possible thanks to PLINQO.

 

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();
}

The code shown here works without changes if your database supports row versioning. If row versioning is not supported in your database, the Update Check policy on each attribute in the dbml must be set to Never for detach to work.

Read more about how PLINQO implemented detach.

Clone

PLINQO takes advantage of the DataContractSerializer in WCF to provide a quality cloning solution for all entity objects. Simply call the Clone method on any entity generated by PLINQO and what you get is an object copied to its own memory location. Below is a quick sample of the clone method in action. A user object is retrieved from the database, cloned, one property is changed, a new user is saved to the database.

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

Serialization of entities has had developers running in circles from the early days of LINQ to SQL until now. PLINQO eliminates this frustration by eliminating the possibility of circular references and taking advantage of WCF datacontract serialization. Here is a sample of serializing an object using PLINQO.

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

 

Deserialization is also made simple with PLINQO

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

Next: Enum Generation