每个 RIA 查询都会创建不同的 EF 对象上下文吗?
我在 Silverlight 应用程序中使用 EF4/RIA 组合。
我的 DomainService
中有多种服务方法。
其中一种方法从数据库中获取一些数据,然后修改对象的值:
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);
}
然后将这些计算值返回到 SL 应用程序。
请注意,在此示例中,实体对象的值已更改。
我有另一个服务方法来更改一些数据,然后调用.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();
}
问题:
我想知道的是,在第二个服务方法中调用SaveChanges
是否会影响在调用第一个服务方法时所做的更改?
或者每个 RIA 查询/服务调用都会创建一个新的 EF ObjectContext?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,是的,每个 RIA 域服务都会被创建、初始化,然后执行您的请求。
因此,新的 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.