views:

389

answers:

2

Hello, I've been wondering for some time if there is any way in Entity Framework to save some changes to the database and others not. Imagine a situation in which I have let's say 4 entity classes - Customer, Task, SalesSchema and Address. Each of those is a customers one-to-many relationship. Now I create a window with 3 tabs (using TabControl) and each of those has a DataGridView that lists adresses, tasks and sales schemas and also for each of those buttons(add, modify, delete) to handle details of each entity type.

What I want to do is to allow partial change commits to database eg. user adds a task -> it's supposed to be saved immediately when the user hits save changes, but only changes from active tab are supposed to be saved. (don't ask me why I have to do this - this is how the client wants it)

It is now achieved by creating separate data contexts and committing each of those separately, but I wonder if there is another way to do this - using one object context.

In ADO.NET datasets there was a way of commiting changes separately - get changes, do some staff that is necessary or even reject the change.

Is this possible with ADO.NET Entity Framework?

A: 

My understanding is that commits in the entity framework are localised to the data context that the entity items are attached to. If you want to do a partial commit while you're working on some other data then you can create a new data context, load the un-related object you need, make the modification and submit on the new data context. Your original data context and object should remain untouched and uncomitted.

Odd
This is really what I do.
kubal5003
+1  A: 

Hi there,

I am not sure if I understand your problem correctly / entirely, but most requirements should be handled with one context easily. Below is a simple example of reverting all objects of a certain type to their original state, if you were to call a SaveChanges() on the context after this - only the remaining objects in added or changed state will commit - a partial commit.

foreach (System.Data.Objects.ObjectStateEntry x in MyEntity.ObjectStateManager.GetObjectStateEntries(EntityState.Added)) // or modified for that matter.
{
   if (x.EntityKey != null)
   {  
      if (x.Entity is MyClass) // look haven't tested this code, merely example and may have typo's
      {
         MyClass tmpObject = (MyClass)x.Entity;
         MyEntity.Refresh(RefreshMode.StoreWins, x);
      }
   }
}

The point I'm trying to get across is there is a lot of control available to you if you track the items you're wanting to "partial" commit. The ones you're wanting to revert to original state or discard can be reverted or even removed completely from the context.

Not sure if this helps any. Best of luck.

Microdot
Thanks. This is best answer so far, so I'm going to mark it as correct. To sum the whole thing up: there is no built in mechanism to do sth. like in datasets. I am aware of workarounds, but what I really wanted to know was if I didn't miss anything.
kubal5003