ObjectContext.Translate-使用不同的属性名称 [英] ObjectContext.Translate - Use different property names

查看:121
本文介绍了ObjectContext.Translate-使用不同的属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Entity Framework并按如下所述调用存储过程: http://msdn.microsoft.com/zh-cn/data/jj691402.aspx 在使用代码访问多个结果集"下

I am using Entity Framework and calling a stored procedure as described here: http://msdn.microsoft.com/en-us/data/jj691402.aspx under "Accessing Multiple Result Sets with Code"

执行存储过程后,我使用ObjectContext.Translate方法将结果放入数据协定对象中,这要求数据协定属性与返回的数据相匹配.

After executing the stored procedure I am using the ObjectContext.Translate method to get my results into a data contract object, which requires that the data contract properties match up to the data returned.

有什么办法可以使用不同的属性名称,但仍然可以使用Translate方法正确地映射它们?

Is there any way I can use different property names, yet still have the Translate method map them correctly?

例如,我有一个获取国家/地区列表的过程,该过程返回以下列:listID,listName.除了这些名称,我希望我的数据协定对象仅具有ID和Name.

For example, I have a procedure for getting a list of countries, which returns the following columns: listID, listName. Instead of those names, I would like my data contract object to have just ID and Name.

我尝试将Name参数添加到DataMember属性,但这没有用.

I tried adding a Name parameter to the DataMember attribute, but that didn't work.

[DataMember(Name="listID")]
public string ID { get; set;}

推荐答案

不,您不能以这种方式进行操作. Translate迭代遍历阅读器内部的数据集,并将其直接映射到您传入的实体中定义的属性.它不知道数据成员注释.这是有道理的,因为数据批注用于序列化,而数据层则不需要知道这些信息.

No, you can not do it this way. Translate iterates through the dataset inside of the reader and maps it directly to the properties defined in the entity you passed in. It does not know about the data member annotations. This makes sense, since the data annotations are used for serialization, which your data layer should not need to know about.

我建议您在具有直接映射到您要查询的属性的实体上调用Translate.然后,让您的DataMember定义将其转换为序列化的名称.例如,如果您的查询返回listID和listName:

I would suggest you call Translate on an entity with the properties that map directly to the query you are calling. Then, have your DataMember define the name that it will be translated to for serialization. For instance, if your query returns listID and listName:

[DataMember("ID")]
public string listID { get; set; }

[DataMember("Name")]
public string listName { get; set; }

这篇关于ObjectContext.Translate-使用不同的属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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