如何在ADO.Net Data Services上使用部分(实体框架)类中定义的只读属性

时间:2022-09-11 18:25:38

I have objects that are defined by the Entity Framework that I have then added additional methods and properties to via partial classes. I think I understand most of the limitations around doing this, but wanted to confirm something I am seeing (or hopefully learn what I need to do to make this work).

我有实体框架定义的对象,然后我通过部分类添加了其他方法和属性。我想我理解了这方面的大部分限制,但是想要确认我所看到的东西(或者希望了解我需要做些什么来使这项工作)。

I have a partial class that then has a read-only property that uses a couple of the items to create a calculated field that is read-only. It was curious to see that read-only property did not come back via the ADO.Net Data Services as I was hoping/expecting. i.e. I was expecting to see the properties on the entity framework and the property being definied in code via the partial class come via the Data Service call.

我有一个部分类,然后有一个只读属性,使用几个项来创建一个只读的计算字段。很奇怪看到只读属性没有像我希望/期望的那样通过ADO.Net数据服务返回。即我希望通过数据服务调用看到实体框架上的属性和通过部分类在代码中定义的属性。

Is this the case? Are the partial classes completely ignored when ADO.Net Data Services is querying for data? If so what is the best practice for getting a read-only type property onto an entity (as I would like to avoid having the same partial classes with different namespaces being cut and pasted into both the client side and server side code bases).

是这样的吗?当ADO.Net Data Services查询数据时,是否完全忽略了部分类?如果是这样的话,将只读类型属性添加到实体的最佳实践是什么(因为我希望避免将具有不同命名空间的相同部分类切割并粘贴到客户端和服务器端代码库中)。

3 个解决方案

#1


3  

From a Microsoft Forums Post: (see full post here)

来自Microsoft论坛帖子:(请参阅此处的完整帖子)

"I think you are asking "How to add a read-only property to an existing entity that is exposed by the EF provider"? In V1, there is no good way to do this. For EF, we load the metadata using the EF metadata api's and hence we don't do any reflection. Hence additional properties that you might have added via a partial class will get ignored.

“我想你问的是”如何向EF提供商公开的现有实体添加只读属性“?在V1中,没有好办法做到这一点。对于EF,我们使用EF加载元数据元数据api,因此我们不做任何反射。因此,您可能通过分部类添加的其他属性将被忽略。

Astoria doesn't yet have a concept of read-only properties. So if we expose any other properties that is not part of the model, we don't know how to deal with them in updates. We don't want to lose data siliently in the server also."

Astoria还没有只读属性的概念。因此,如果我们公开任何不属于模型的其他属性,我们就不知道如何在更新中处理它们。我们也不想在服务器中轻易丢失数据。“

So it looks like this is functionality that is not able to be exposed via ADO.Net Data Services.

所以看起来这是无法通过ADO.Net数据服务公开的功能。

#2


2  

There are two separate concerns here - the base model (EF), and the WCF/mex layer. Your additional properties will not be part of the edmx model, but I wonder if the issue isn't more related to the WCF/mex aspect.

这里有两个不同的问题 - 基本模型(EF)和WCF / mex层。您的其他属性将不是edmx模型的一部分,但我想知道该问题是否与WCF / mex方面无关。

However, even if it worked, ADO.NET Data Services transfers data, not logic. So relying on calculated properties isn't a safe option : the client won't have the formulae - just the original values.

但是,即使它工作,ADO.NET数据服务也会传输数据,而不是逻辑。所以依赖计算属性不是一个安全的选择:客户端不会有公式 - 只是原始值。

To find out which it is, try making the property read/write (even if the write doesn't do anything useful), and ensure the value has a [DataMember] attribute, etc.

要找出它是什么,尝试使属性读/写(即使写没有做任何有用的事情),并确保该值具有[DataMember]属性等。

#3


-1  

I think the problem is that with XML serialization, it only serializes properties with get and set methods. Otherwise it couldn't de-serialize. Add an empty set method to your property and see how you go.

我认为问题在于使用XML序列化,它只使用get和set方法序列化属性。否则它无法反序列化。添加一个空的set方法到您的属性,看看你如何去。

Rob

#1


3  

From a Microsoft Forums Post: (see full post here)

来自Microsoft论坛帖子:(请参阅此处的完整帖子)

"I think you are asking "How to add a read-only property to an existing entity that is exposed by the EF provider"? In V1, there is no good way to do this. For EF, we load the metadata using the EF metadata api's and hence we don't do any reflection. Hence additional properties that you might have added via a partial class will get ignored.

“我想你问的是”如何向EF提供商公开的现有实体添加只读属性“?在V1中,没有好办法做到这一点。对于EF,我们使用EF加载元数据元数据api,因此我们不做任何反射。因此,您可能通过分部类添加的其他属性将被忽略。

Astoria doesn't yet have a concept of read-only properties. So if we expose any other properties that is not part of the model, we don't know how to deal with them in updates. We don't want to lose data siliently in the server also."

Astoria还没有只读属性的概念。因此,如果我们公开任何不属于模型的其他属性,我们就不知道如何在更新中处理它们。我们也不想在服务器中轻易丢失数据。“

So it looks like this is functionality that is not able to be exposed via ADO.Net Data Services.

所以看起来这是无法通过ADO.Net数据服务公开的功能。

#2


2  

There are two separate concerns here - the base model (EF), and the WCF/mex layer. Your additional properties will not be part of the edmx model, but I wonder if the issue isn't more related to the WCF/mex aspect.

这里有两个不同的问题 - 基本模型(EF)和WCF / mex层。您的其他属性将不是edmx模型的一部分,但我想知道该问题是否与WCF / mex方面无关。

However, even if it worked, ADO.NET Data Services transfers data, not logic. So relying on calculated properties isn't a safe option : the client won't have the formulae - just the original values.

但是,即使它工作,ADO.NET数据服务也会传输数据,而不是逻辑。所以依赖计算属性不是一个安全的选择:客户端不会有公式 - 只是原始值。

To find out which it is, try making the property read/write (even if the write doesn't do anything useful), and ensure the value has a [DataMember] attribute, etc.

要找出它是什么,尝试使属性读/写(即使写没有做任何有用的事情),并确保该值具有[DataMember]属性等。

#3


-1  

I think the problem is that with XML serialization, it only serializes properties with get and set methods. Otherwise it couldn't de-serialize. Add an empty set method to your property and see how you go.

我认为问题在于使用XML序列化,它只使用get和set方法序列化属性。否则它无法反序列化。添加一个空的set方法到您的属性,看看你如何去。

Rob