更新过程运行后如何立即重新查询更新的值 [英] How to requery updated values immediately after update procedure runs
问题描述
据我所知,以下代码对"MySite"进行的数据库更改是立即进行的:
As far as I can tell, changes made to the database by the following code to "MySite" are immediate:
public List<vcData> UpdateDisplayAndUrl(List<vcData> vcDataList)
{
foreach (vcData vcData in vcDataList)
{
_entities.ExecuteStoreCommand("UPDATE vcData SET DisplayItem = {0}, DisplayUrl = {1} WHERE ID = {2} ", vcData.DisplayItem, vcData.DisplayUrl, vcData.ID);
}
return GetTableData();
}
我想做的是立即返回新更新的记录以进行进一步处理,本质上是重新查询刚刚进行的更改:
What I would like to do is to immediately return the newly updated records for further processing, essentially, re-query the changes that have just been made:
public List<vcData> GetTableData()
{
var result = (from td in _entities.vcData
where td.SiteID == "MySite"
select td).ToList();
return result;
}
在我的控制器代码中,我正在尝试执行以下操作:
In my controller code, I am trying to do something like:
_currentvcTickerDataList = Repository.UpdateDisplayAndUrl(vcUnupdatedDataList);
//.....do more stuff with _currentvcTickerDataList, which ought to contain updated information, should it not?
问题:GetTableData()
方法似乎不返回更新后的值,而仅返回先前(未更新)的值.
PROBLEM: the GetTableData()
method does not seem to return the updated values, only the previous (unupdated) values.
我是LINQ,实体框架和MVC的新手,所以我可以肯定我缺少一些基本知识.
I am new to LINQ, entity framework, and MVC, so I'm pretty certain there is something fundamental that I am missing.
推荐答案
在查询之前,您需要将MergeOption
设置为OverwriteChanges
.请仔细阅读 msdn 中所述的MergeOption,有个清楚的主意.
You need to set the MergeOption
to OverwriteChanges
before querying. Please go through the MergeOptions explained in msdn to get a clear idea.
public List<vcData> GetTableData()
{
var currentMergeOption = _entities.vcData.MergeOption;
_entities.vcData.MergeOption = MergeOption.OverwriteChanges;
var result = (from td in _entities.vcData
where td.SiteID == "MySite"
select td).ToList();
//revert the change
_entities.vcData.MergeOption = currentMergeOption;
return result;
}
这篇关于更新过程运行后如何立即重新查询更新的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!