1

I have the following Post Edit action method:-

 [HttpPost]
 [ValidateAntiForgeryToken]
 [CheckUserPermissions(Action = "Edit", Model = "StorageDevice")]
        public ActionResult Edit(SDJoin sdj, FormCollection formValues)
        {
            //code goes here

                if (ModelState.IsValid)
                {

                    repository.Save();
            catch (DbUpdateConcurrencyException ex)
            {
var entry = ex.Entries.Single();
var databaseValues = (TMSStorageDevice)entry.GetDatabaseValues().ToObject();
var clientValues = (TMSStorageDevice)entry.Entity;

var databaseTechnology2 = repository.FindTechnology2(sdj.StorageDevice.TMSStorageDeviceID);

if (sdj.NetworkInfo.IPAddress != databaseTechnology2.TechnologyIPs.SingleOrDefault(a=>a.IsPrimary == true).IPAddress )

                    ModelState.AddModelError("NetworkInfo.IPAddress", "Value Has Changed "
                           );
                if (sdj.NetworkInfo.MACAddress != databaseTechnology2.TechnologyIPs.SingleOrDefault(a => a.IsPrimary == true).MACAddress)

                    ModelState.AddModelError("NetworkInfo.MACAddress", "Value Has Changed "
                           );
                if (databaseValues.RackID != clientValues.RackID)

                ModelState.AddModelError("StorageDevice.RackID", "Value Has Changed "
                       );

But currently the values returned from the

var databaseTechnology2 = repository.FindTechnology2(sdj.StorageDevice.TMSStorageDeviceID);

will return a cached value inside the server , instead of retrieving the current database value. The repository method is :-

public Technology FindTechnology2(int id)
        {

            return tms.Technologies.Include(a=>a.TechnologyIPs).SingleOrDefault(a => a.TechnologyID == id);
        }

Can anyone advice ?

john Gu
  • 10,469
  • 55
  • 189
  • 381
  • It might be an issue in which you're querying on the same context. You can try to solve it using MergeOption.OverwriteChanges as described here (http://stackoverflow.com/questions/6126845/refreshing-data-using-entity-framework) – lopezbertoni Feb 12 '14 at 12:16
  • I tried writting somthing such as "ObjectQuery query = entities.TechnologyRoles;" but i got the following error :Cannot implicitly convert type 'System.Data.Entity.DbSet' to 'System.Data.Objects.ObjectQuery' – john Gu Feb 12 '14 at 12:49

1 Answers1

0

I'm posting this as a follow up to my comment in order to show code properly.

You can set MergeOptions.OverwriteChanges as follows:

var objectContext = ((IObjectContextAdapter)entities).ObjectContext; //assuming 'entities' is your context here
var set = objectContext.CreateObjectSet<TechnologyRoles>();
set.MergeOption = MergeOption.OverwriteChanges;
var query = set.SingleOrDefault(a => a.TechnologyID == id);

Hope this helps,

lopezbertoni
  • 3,146
  • 3
  • 36
  • 50
  • Can you try using the Refresh method? http://msdn.microsoft.com/en-us/library/bb896255.aspx – lopezbertoni Feb 12 '14 at 13:34
  • not sure whereto apply the Refersh inside my code ? – john Gu Feb 12 '14 at 13:58
  • you can check the provided link as well as this link. http://stackoverflow.com/questions/2564265/entity-framework-refresh-objects-from-database – lopezbertoni Feb 12 '14 at 15:43
  • I use it as follow :- return tms.Technologies.AsNoTracking().Include(a=>a.TechnologyIPs).SingleOrDefault(a => a.TechnologyID == id); . and it worked well, not sure if this is the right way to solve my problem ? any idea ? – john Gu Feb 13 '14 at 00:26
  • AsNoTracking() means that the entities will not be cached locally by the ObjectContext instance. So in this case, you're bypassing the cache and therefore you're getting the results you want. However, I wonder if you update your entities, the results will persist. You can refer to this answer (http://stackoverflow.com/questions/14527721/asnotracking-in-entityframework) – lopezbertoni Feb 13 '14 at 17:52