0

I have a Data Access Layer which creates a context and retrieves data (with no object tracking) and passes the information back to UI layer:-

My unit of work is a method and I release appdatacontext after executing the particular method. So I am not keeping track of the data context anywhere..

public  LinqObject GetObject(){
  using (appdatacontext = new DataContext()){ 

---code to select and return object
   }
}

I will modify data using the form in UI and submit back my data to DB.

Two approaches are:-

1. Detach and reattach to a different data context using [Detach..Serialise and Attach]

 *I have to do a lot of plumping code to enable this functionality* 

2. Get the DB object using primary key and make changes in the selected object and SubmitChanges.

Which one is a better approach for doing this task?

I am completely against moving the unit of work to Data Access Layer wise or Web Application Life cycle (httpcontext), because I dont want to track the changes and complicate the entire application structure with unwanted plumping code . I am just using LINQ for making my retrieval and updates to DB easy.

I have never seen anyone discuss these two approaches in LINQ context, that is why I am asking for best practice.

Rohith Nair
  • 1,080
  • 1
  • 17
  • 33
  • I suggest you check [This](http://www.codeproject.com/Articles/33088/Reattaching-Entity-Graphs-with-the-Entity-Framewor). It is your first option solution. I had to use this in EF 3.5. I don't know if this is solved in version 4.0 and higher. – noobob May 09 '13 at 12:16

2 Answers2

1

If you don't want to go with (2) because of performance: another option is to attach a new object to submit the updates.

Foo foo=new Foo { FooId=fooId }; // create obj and set keys
context.Foos.Attach(foo);
foo.Name="test";
context.SubmitChanges();

See my answer here.

Community
  • 1
  • 1
laktak
  • 57,064
  • 17
  • 134
  • 164
  • I tried this and is working, as you know I have to manually go on and assign every value before submit changes. How can I copy my entity values in one go and submit the changes? I tried Cloning (DatacontractSerializer) my passed object and assigning it, but its not working. Any solution? – Rohith Nair May 10 '13 at 09:35
  • You could do that via reflection. – laktak May 10 '13 at 10:02
0

As per @Chris's comments. I have finally arrived at the solution like:-

function void SaveRow(Table.RowObject object) {

  var original=null;

  using (context= new DataContext())
        {

            context.ObjectTrackingEnabled = false;
            original = {query}.Single();

        }
        using(context=new DataContext()){
            try
            {
                context.Table.Attach(object, original);
                context.SubmitChanges();
            }
            catch (Exception exception) {

                saveStatus = false;

            }
        }

}

I kept the update checks to ensure there is concurrency checking, if I disable that I can reduce the amount of where statement generated by Linq.

Rohith Nair
  • 1,080
  • 1
  • 17
  • 33