EF4 + STE:通过 WCF 服务重新连接?每次都使用新的对象上下文?

发布于 2024-10-11 12:47:15 字数 659 浏览 12 评论 0原文

我计划将 WCF(不是 ria)与实体框架 4 和 STE(自跟踪实体)结合使用。如果我正确理解这一点,我的 WCF 应该将一个实体或实体集合(例如使用 LIST 而不是 IQueryable)返回给客户端(在我的例子中是 Silverlight)。

然后,客户端可以更改实体或更新它。此时我相信它是自我跟踪?这是我有点困惑的地方,因为有很多报告的 STE 没有跟踪的问题。

无论如何,要更新,我只需使用另一种方法将实体发送回我的 WCF 服务即可进行更新。我应该每次都创建一个新的 OBJECTCONTEXT 吗?在每个方法中?

如果我每次在 WCF 上的每个方法中创建一个新的对象上下文,那么我不需要将 STE 重新附加到对象上下文吗?

所以基本上仅靠这个是行不通的?

using(var ctx = new MyContext())
{
    ctx.Orders.ApplyChanges(order);
    ctx.SaveChanges();
}

或者我应该在 WCF 服务的构造函数中创建一次对象上下文,以便使用同一 WCF 实例的 1 个调用和每个附加调用都使用相同的对象上下文?

我可以在客户端的每个方法调用中创建和销毁 WCF 服务 - 因此每次都会创建一个新的对象上下文。

我知道让对象上下文长时间保持活动状态并不是一个好主意。

I am planning to use WCF (not ria) in conjunction with Entity Framework 4 and STE (Self tracking entitites). If I understand this correctly my WCF should return an entity or collection of entities (using LIST for example and not IQueryable) to the client (in my case Silverlight).

The client then can change the entity or update it. At this point I believe it is self tracking? This is where I sort of get a bit confused as there are a lot of reported problems with STEs not tracking.

Anyway, then to update I just need to send back the entity to my WCF service on another method to do the update. I should be creating a new OBJECTCONTEXT every time? In every method?

If I am creating a new objectcontext every time in every method on my WCF then don't I need to re-attach the STE to the objectcontext?

So basically this alone wouldn't work??

using(var ctx = new MyContext())
{
    ctx.Orders.ApplyChanges(order);
    ctx.SaveChanges();
}

Or should I be creating the object context once in the constructor of the WCF service so that 1 call and every additional call using the same WCF instance uses the same objectcontext?

I could create and destroy the WCF service in each method call from the client - hence creating in effect a new objectcontext each time.

I understand that it isn't a good idea to keep the objectcontext alive for very long.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请别遗忘我 2024-10-18 12:47:15

您提出了几个问题,因此我将尝试分别回答它们:

返回 IQueryable:

您无法返回 IQueryalbe。 IQueryable 描述应该执行的查询。当您尝试从服务返回 IQueryable 时,它​​将在服务响应序列化期间执行。它通常会导致异常,因为 ObjectContext 已经关闭。

客户端跟踪:

是的,如果客户端使用 STE,STE 可以跟踪客户端上的更改! STE 的装配应在服务和客户端之间共享。

共享ObjectContext:

切勿在更新数据的服务器环境中共享ObjectContext。始终为每次调用创建新的 ObjectContext 实例。我在此处描述了原因。

附加 STE

您不需要附加 STE。 ApplyChanges 将为您做一切。此外,如果您想从服务操作中退回订单,您应该对其调用 AcceptChanges 。

在服务构造函数中创建对象上下文:

请注意,WCF 对于如何使用服务实例有自己的规则。这些规则基于 InstanceContextMode 并使用绑定(并且您可以通过实现 IInstanceProvider 来实现您自己的规则)。例如,如果您使用 BasicHttpBinding,默认实例化将为 PerCall,这意味着 WCF 将为每个请求创建新的服务实例。但如果您使用 NetTcpBinding,则默认实例化将为 PerSession,并且 WCF 将为来自单个客户端(单个客户端代理实例)的所有请求重用单个服务实例。

在客户端上重用服务代理:

这也取决于使用的绑定和服务实例。当使用面向会话的绑定时,客户端代理与单个服务实例相关。在该代理上调用方法将始终在同一服务实例上执行操作,因此服务实例可以是有状态的(可以包含在调用之间共享的数据)。这通常不是一个好主意,但它是可能的。当使用面向会话的连接时,您必须处理可能出现的几个问题(更复杂)。 BasicHttpBinding 不允许会话,因此即使使用单个客户端代理,每个调用都由新的服务实例处理。

You are asking several questions so I will try to answer them separately:

Returning IQueryable:

You can't return IQueryalbe. IQueryable describes query which should be executed. When you try to return IQueryable from service it is being executed during serialization of service response. It usually causes exception because ObjectContext is already closed.

Tracking on client:

Yes STEs can track changes on a client if client uses STEs! Assembly with STEs should be shared between service and client.

Sharing ObjectContext:

Never share ObjectContext in server environment which updates data. Always create new ObjectContext instance for every call. I described reasons here.

Attaching STE

You don't need to attach STE. ApplyChanges will do everything for you. Also if you want to returen order back from your service operation you should call AcceptChanges on it.

Creating object context in service constructor:

Be aware that WCF has its own rules how to work with service instances. These rules are based on InstanceContextMode and used binding (and you can implement your own rules by implement IInstanceProvider). For example if you use BasicHttpBinding, default instancing will be PerCall which means that WCF will create new service instance for each request. But if you use NetTcpBinding instead, default instancing will be PerSession and WCF will reuse single service instance for all request comming from single client (single client proxy instance).

Reusing service proxy on a client:

This also depends on used binding and service instancing. When session oriented binding is used client proxy is related to single service instance. Calling methods on that proxy will always execute operations on the same service instance so service instance can be stateful (can contains data shared among calls). This is not generally good idea but it is possible. When using session oriented connection you have to deal with several problems which can arise (it is more complex). BasicHttpBinding does not allow sessions so even with single client proxy, each call is processed by new service instance.

梦途 2024-10-18 12:47:15

您可以将实体附加到新的对象上下文,请参阅 http://msdn.microsoft .com/en-us/library/bb896271.aspx

但是,它的状态将保持不变。

我这样做的方法是:

  • 重新查询数据库以获取信息
  • ,将其与发送的对象进行比较,
  • 使用更改更新数据库中的实体
  • 然后进行正常的保存更改

编辑

以上是针对POCO,正如评论中指出的

对于 STE,您每次都会创建一个新上下文,但使用“ApplyChanges”,请参阅:

You can attach an entity to a new object context, see http://msdn.microsoft.com/en-us/library/bb896271.aspx.

But, it will then have the state unchanged.

The way I would do it is:

  • to requery the database for the information
  • compare it with the object being sent in
  • Update the entity from the database with the changes
  • Then do a normal save changes

Edit

The above was for POCO, as pointed out in the comment

For STE, you create a new context each time but use "ApplyChanges", see: http://msdn.microsoft.com/en-us/library/ee789839.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文