I am using EF4/RIA combo in a Silverlight application.
I have multiple service methods in my DomainService
.
One of the methods fetches some data from the database and then modifies the objects' values:
IEnumerable<Factor> GetModifiedFactors(double threshold)
{
List<Factor> factors = ObjectContext.Where(f => f.Id == selectedId).ToList();
for(int i = 1; i < factors.Count; i++)
{
Factor current = factors[i];
Factor previous = factors[i - 1];
// Note that here the value of the entity object has been changed
current.Value = 2 * current.Value - 3 * previous.Value;
}
return factors.Where(f => f.Value > threshold);
}
These calculated values are then returned to the SL application.
Note in this example that the entity object's value has been changed.
I have another service method that changes some data and then calls .SaveChanges()
.
[Invoke]
public void ResetFactor(int factorId, double defaultValue)
{
Factor factor = ObjectContext.Factors.FirstOrDefault(f => f.Id == factorId);
if(factor == null)
return;
factor.Value = defaultValue;
ObjectContext.SaveChanges();
}
Question:
What I want to know is whether this call to SaveChanges
in the second service method will affect changes made in the call to the first service method?
Or does every RIA query/service-call create a new EF ObjectContext?
By default, yes, every RIA Domain Service is created, initialized and then it executes your request.
So new ObjectContext will anyway fetch objects directly from database, so it will contain changes made by other service.