在客户端公开自定义服务器端实体框架属性

发布于 2024-09-15 19:58:04 字数 695 浏览 3 评论 0原文

我正在使用 WCF RIA 服务制作 Silverlight 4 应用程序。

在服务器端(*.Web 项目),我有一个从 SQL Server 数据库自动生成的实体模型。 在客户端,我拥有由 Visual Studio 生成的域服务和代理对象,以便在 Silverlight 程序集中使用。

我想向模型添加自定义属性(最好在服务器端)。 假设我有 ContactCompanyAddress 表,它们通过外键链接(但不一定是实际的外键约束 /em>)。我想添加一个属性,该属性将返回 ContactCompanyAddress 对象。

我一直在尝试通过创建一个分部类来扩展 Contact 类,并添加一个 CompanyAddress { get; 来实现此目的。 } 属性。但我不知道需要如何处理新属性才能使其传播到客户端自动生成的代码。我必须向该属性添加特定属性吗?我是否必须在某处注册它以便代码生成器知道它?

这必须是导航属性还是可以更简单?

这是否是最好的做事方式,或者我应该放弃扩展服务器端模型而只在客户端进行? (如果我在客户端执行此操作,我将面临无法访问各个 Entity 派生类内的上下文对象的问题。)

I'm making a Silverlight 4 application with WCF RIA Services.

On the server side (the *.Web project), I have an Entity Model that's auto-generated from a SQL Server database.
On the client side, I have the domain service and proxy objects that are generated by Visual Studio for use in Silverlight assemblies.

I want to add custom properties to the model (preferably on the server side).
Say I have Contact, Company, and Address tables, which are linked by foreign keys (but not necessarily actual foreign key constraints). I want to add a property that will return a Contact's Company's Address object.

I have been attempting to do this by making a partial class to extend the Contact class, and adding a CompanyAddress { get; } property. But I have no idea what I need to do with the new property in order to make it propagate to the auto-generated code on the client side. Are there specific attributes I have to add to the property? Do I have to register it somewhere so that the code generator will know about it?

Does this have to be a Navigation Property or can it be something simpler?

And is this even the best way to do things, or should I give up on extending the server-side model and just do it on the client side? (If I do it on the client side, I face the problem of not having access to the context object inside the individual Entity-derived classes.)

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

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

发布评论

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

评论(1

旧夏天 2024-09-22 19:58:04

我从未使用过 Silverlight 或 RIA 服务,但我想它们会非常相似。当您创建 EF 模型并且具有通过外键关联的实体(必须存在关系)时,与其他实体相关的每个实体将包含称为导航属性的内容。因此,在您的场景中,“联系人”应包含名为“公司”的属性,而“公司”应包含名为“地址”的属性。您可以通过使用 ObjectSet 上的 Include 或延迟加载(在 WCF 中不是好主意)来让 EF 加载这些导航属性。如果您通过 WCF 将联系人发送给客户,公司和地址也将被发送。

你的方法有一个大问题。您的属性仅包含 getter - 这样的属性未序列化。

I have never used Silverlight or RIA services but I guess it will be quite similar. When you create EF model and you have entities related by foreign key (there has to be relation), each entity related to other entity will contain something called navigation property. So in your scenario Contact should contain property named Company and Company shoud contain property called Address. You can isntruct EF to load those navigation properties by using Include on ObjectSet or by lazy loading (not good idea in WCF). Than if you send Contact by WCF to the client, Company and Address will be send as well.

Your approach has one big problem. Your property contains only getter - such property is not serialized.

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