可以编辑服务器端吗?
我有一个表格,其中值以编程方式组成,值一直在变化,因此我希望“更新”重新计算表的值并提交更改。我不需要将任何数据传回客户端。
在更改了相关记录后,我在ObjectContext.SaveChanges()
中尝试了DomainServiceClass
方法,但它没有更新数据库值。
我错过了什么?有可能吗?
SERVER SIDE,DomainServiceClass方法:
public IQueryable<POINT> UpdateDB()
{
var Points = ObjectContext.POINTS.Include("Readings").Include("Items").Include("LatestStatus");
List<POINT> itemsChanged = new List<POINT>();
foreach (Point point in Points)
{
int status = CalculateStatus(point)
if (point.LatestStatus.FirstOrDefault().Status != status)
point.LatestStatus.FirstOrDefault().Status = new status
}
ObjectContext.SaveChanges();
/*try
{
// Try to save changes, which may cause a conflict.
int num = ObjectContext.SaveChanges();
Console.WriteLine("No conflicts. " +
num.ToString() + " updates saved.");
}
catch (OptimisticConcurrencyException)
{
// Resolve the concurrency conflict by refreshing the
// object context before re-saving changes.
ObjectContext.Refresh(RefreshMode.ClientWins, inspectionPoints);
// Save changes.
ObjectContext.SaveChanges();
Console.WriteLine("OptimisticConcurrencyException "
+ "handled and changes saved");
}*/
return itemsChanged.AsQueryable();
}
答案 0 :(得分:0)
由于您尝试进行的更改不是传统的CRUD操作,因此您需要使用invoke属性标记方法。对POINT对象的更新也需要使用ObjectContext,否则你只需更改内存集合。
[Invoke]
public IQueryable<POINT> UpdateDB()
{
var Points = ObjectContext.POINTS.Include("Readings").Include("Items").Include("LatestStatus");
var itemsChanged = new List<POINT>();
foreach (Point point in Points)
{
int status = CalculateStatus(point);
if (point.LatestStatus.FirstOrDefault().Status != status) // Where does this status come from?
{
ObjectContext.POINTS.FirstOrDefault( p => p.ID == point.ID).LatestStatus.FirstOrDefault().Status = new status // Pseudo code?
//point.LatestStatus.FirstOrDefault().Status = new status
}
}
ObjectContext.SaveChanges();
}
然后你可以调用这个方法:
var ctx = new YourDomainContext();
ctx.UpdateDB( (op) =>
{
if (op.HasError)
// Handle errors.
else
{
// Do stuff.
}
});