Question

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;}
Was it helpful?

Solution

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; }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top