自跟踪实体在我的 WCF 服务中返回不变

发布于 2024-09-10 05:16:33 字数 1575 浏览 13 评论 0原文

我在前端使用 MVP(模型视图演示器)设置了一个小型 n 层 Web 应用程序,在后端使用 WCF 服务,它与 BLL 通信,后面的 DAL 与 EF4.0 通信。 这一切都工作得很好,我创建了几个都可以工作的 Get 和 Add 方法。 现在我想创建几个 Update 方法。由于我使用的是 EF4.0 的自我跟踪实体 (STE),因此我认为一切都会顺利进行,而且实施起来也会相当容易。

事实似乎并非如此。 当在 Web 前端更改我的项目时,当移回 WCF 服务时,它们的状态保持不变。由于实体未更改,因此数据库中不会更新任何内容。 通过这个问题: EF 4 自我跟踪实体确实没有按预期工作 我读到这篇文章,其中更详细地介绍了有关在 n 层应用程序中实现 STE 的信息。 (http://msdn.microsoft.com/en-us/magazine/ee335715 .aspx) 我引用:

确保重复使用自我跟踪实体模板的 在您的上生成的实体代码 客户端。如果您使用代理代码 由添加服务引用生成 Visual Studio 或其他一些工具, 大部分情况看起来都不错, 但你会发现 实体实际上并不跟踪 他们在客户端上的更改。

好吧,我已经设法使用以下代码创建了该服务的客户端:

var svc = new ChannelFactory<INorthwindSTEService>(
    "INorthwindSTEService")
    .CreateChannel();

这似乎工作得很好。 问题是,如何在客户端获取序列化实体? 在代码示例中,我看到作者使用了以下代码:

var products = new List<Product>(svc.GetProducts());
var customer = svc.GetCustomer("ALFKI");

但我看不到那些对象/实体/???已定义(我正在谈论客户产品

我已经使用使用添加服务引用创建的实体对其进行了测试>,但这似乎不起作用。返回的实体仍具有未更改状态。

我不想在前端引用 EF 实体,因为我希望将它们分开,并且在前端仅包含序列化对象。不过,如果真的有必要的话,我可能不得不这样做。

哦,在实体对象上使用 MarkAsModified() 方法确实可以很好地工作,但这更像是一种黑客行为,因为实体应该自行检查其状态。

关于如何进行有什么建议吗? 我的猜测是使用 MarkAsModiefied() 方法或在我的 Web 应用程序中引用实体模型项目可能是可行的解决方法,但如果有更好的方法,我想先尝试一下。

I've set up a little n-tier web application using MVP (Model View Presenter) in the front-end, a WCF service on the backend, which communicates with the BLL an behind that the DAL, which communicates with the EF4.0.
This is all working quite nicely, I've created several Get and Add methods which all work.
Now I wanted to create several Update methods. Since I'm using the Self-Tracking Entities (STE) of the EF4.0, I thought everything would work out quite fine and implementation would be rather easy.

This doesn't seem to be the case.
When changing my items in the web front-end, they keep having the status Unchanged when moving back to the WCF-service. Because the entity is unchanged, nothing gets updated in the database.
Through this question on SO: EF 4 Self Tracking Entities does not work as expected
I came accross the article telling something more in detail about implementing the STE's with in an n-tier application. (http://msdn.microsoft.com/en-us/magazine/ee335715.aspx)
I quote:

Make certain to reuse the Self-Tracking Entity template’s
generated entity code on your
client.
If you use proxy code
generated by Add Service Reference in
Visual Studio or some other tool,
things look right for the most part,
but you will discover that the
entities don’t actually keep track of
their changes on the client.

Well, I've managed to create a client to the service using this code:

var svc = new ChannelFactory<INorthwindSTEService>(
    "INorthwindSTEService")
    .CreateChannel();

This appears to work quite alright.
The thing is, how do I get the serialized entities on the client?
In the code example I'm seeing the writer uses this code:

var products = new List<Product>(svc.GetProducts());
var customer = svc.GetCustomer("ALFKI");

But I fail to see where those objects/entities/??? are defined (I'm talking about a Customer and a Product)

I've tested it using the entities which were created using the Add Service Reference, but that doesn't seem to work. The returned entities still have the status Unchanged.

I'd rather not reference the EF entities in my front-end, because I'd like to keep them separated and only have serialized objects on my front-end. Though, if it is really necessary, I might just have to do that.

Oh, using the MarkAsModified() method on the entity object does work nicely though, but that's more of a hack as the entity should check his status by itself.

Any advice on how to proceed?
My guess is using the MarkAsModiefied() method or referencing the entity models project in my web application could be viable work-around, but if there's something better, I'd like to try that first.

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

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

发布评论

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

评论(1

欢烬 2024-09-17 05:16:33

您需要将自跟踪实体移出实体框架项目,

如果您查看 .tt 文件,您将看到有一个您必须填写的 .edmx 文件的路径。

这将允许您将它们放入其中一个独立于实体框架的项目。

然后,您可以从前端引用该项目,并按照您的预期访问跟踪实体。

至于执行更新……我自己看;p
(我真的不想自己将它们筛选到不同的状态)

你这样说
“确保在您的客户端上重用自我跟踪实体模板生成的实体代码”,

然后您说您不想在底层执行此操作。

那是你的问题。

You need to move the self tracking entities out of the entity frameowrk project

if you look in the .tt file you will see that there is a path to to .edmx file that you ahve to fill in.

This will aloow you to have them in a project seperate to the entity framework.

you can then reference this project from your front end and have access to the tracking entities as you expect.

As for performing the update ... well im looking myself ;p
(I don't really want to sieve them out into their different states myself)

you say this
'Make certain to reuse the Self-Tracking Entity template’s generated entity code on your client'

and then you say that you do not want to do this lower down.

That is your problem.

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