ObjectContext.Translate - Use different property names

时光怂恿深爱的人放手 提交于 2019-12-10 23:52:04

问题


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"

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.

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

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.

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

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

回答1:


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.

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; }


来源:https://stackoverflow.com/questions/15038285/objectcontext-translate-use-different-property-names

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!