Can edits be made server side?
I have a table where the values are made up programmatically, the values change all the time and so I would like an 'update' to re-calculate the values for the table and commit the changes. I do not need to have ANY data passed back to the client side.
I have tried the ObjectContext.SaveChanges()
method in my DomainServiceClass
after changes the relevent records, but it doesn't chnage the database values.
What have I missed? Is it possible?
SERVER SIDE, DomainServiceClass method:
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();
}
As the change you are trying to make is not a traditional CRUD operation you need to mark your method with the invoke attribute. Also updates onto your POINT objects need to use the ObjectContext otherwise you're just changing your in memory collection.
[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();
}
You could then call this method:
var ctx = new YourDomainContext();
ctx.UpdateDB( (op) =>
{
if (op.HasError)
// Handle errors.
else
{
// Do stuff.
}
});