如何使用WCF数据服务公开非持久属性? [英] How do I expose non persisted properties using a WCF Data Service?

查看:60
本文介绍了如何使用WCF数据服务公开非持久属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我使用Entity Framework 4创建了一个实体模型,该实体模型是通过WCF数据服务公开的.我的一个实体需要定义无法持久保存到数据库中的属性,但是实体模型设计器不允许您执行此操作.

为了解决这个问题,我将所有对象都定义为POCO对象,这使您可以向对象添加非持久属性,但不能向模型添加属性.

我遇到的问题是,因为这些非持久属性仅存在于对象本身而不是模型中,所以它们不会通过WCF数据服务公开.

有什么方法可以在实体模型中定义不持久化到数据库的属性?

预先感谢您的任何答复

Ryan

Hi all,

I''ve created an entity model using Entity Framework 4, which I''ve exposed via a WCF Data Service. One of my entities needs to have properties defined that are not persisted to the database, but the Entity Model designer doesn''t allow you to do this.

To get round this I''ve defined all my objects as POCO objects, which allows you to add non persisted properties to your objects, but not your model.

The issue I have is that, because these non persisted properties only exist in the objects themselves and not the model, they are not exposed via the WCF Data Service.

Is there any way to define properties in an entity model that are not persisted to the database?

Thanks in advance for any replies

Ryan

推荐答案

答案是肯定的:您可以向EF模型添加非持久属性,但不能通过设计器.
生成的模型都是partial类,这意味着您可以将完整的类实现分布在多个文件中.假设您有一个名为Foo的实体.您可以像这样将文件添加到名为Foo.cs的项目中:


The answer is yes: You can add non-persistent properties to an EF model, but not through the designer.
The generated models are all partial classes, which means you can spread the full class implementation over several files. Let''s say you have an Entity called Foo. You can add a file to the project called Foo.cs like this:


namespace MyNameSpace
{
  public partial class Foo
  {
    public string MyNonPersistantProperty {get; set; }
  } 
}



请注意,类型名称和名称空间名称都必须与EF类中的名称和名称相匹配. [edit]您应该能够将它们标记为数据合同属性.抱歉,我只回答了您问题的最后一部分.



Note that both the type name and namespace name must match the one in the EF class. [edit] You should be able to mark these up as data contract properties. I only really answered the final part of your question, sorry!


Microsoft自从发布了EF4.1和新版本的WCF数据服务(当前在CTP中)以来,您就可以公开通过数据服务保留属性.

为此,您需要使用代码优先方法定义实体模型,该方法不会生成EDMX文件.您可以使用数据注释或流利的API来指定不保留属性.如果您首先与设计人员一起使用模型方法,那么不幸的是,数据服务将使用生成的EDMX文件来确定要公开的属性.
Microsoft have since released EF4.1 and a new version of WCF Data Services (currently in CTP) which allow you to expose non persisted properties via your data service.

To achieve this you need to define your entity model using the code first approach, which doesn''t generate an EDMX file. You can use data annotations or the fluent API to specify that properties are not persisted. If you use the model first approach with the designer, then unfortunately, the data service will use the generated EDMX file to determine what properties to expose.


这篇关于如何使用WCF数据服务公开非持久属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆