正在缓存从我的Repository模型类返回的值

时间:2014-02-12 12:03:14

标签: asp.net asp.net-mvc linq entity-framework asp.net-mvc-4

我有以下Post Edit操作方法: -

 [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 "
                       );

但目前从

返回的值
var databaseTechnology2 = repository.FindTechnology2(sdj.StorageDevice.TMSStorageDeviceID);

将在服务器内返回缓存值,而不是检索当前数据库值。存储库方法是: -

public Technology FindTechnology2(int id)
        {

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

有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

我发布此信息作为我评论的后续内容,以便正确显示代码。

您可以按如下方式设置MergeOptions.OverwriteChanges:

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

希望这有帮助,