ObjectContext.Translate - Use different property names
Asked Answered
C

1

3

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;}
Cynicism answered 23/2, 2013 at 7:33 Comment(0)
F
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; }
Fayfayal answered 3/4, 2013 at 14:15 Comment(1)
We are sharing data contracts between the client(front end) and a service/data layer, so we ended up just using the column names that the stored procedure returns.Cynicism

© 2022 - 2024 — McMap. All rights reserved.