none
while exposing Entity Framework object as datacontract how to remove some datamembers present in the Datacontract RRS feed

  • Question

  • while exposing Entity Framework object as datacontract how to remove some datamembers present in the Datacontract

    For example i dont want to expose fields like  Modifiedby ,ModifiedDate which as part of the conceptual schema of the Entity framework.


    ilikemicrosoft
    Monday, August 29, 2011 5:46 AM

Answers

  • Hello,

    you must mark entity class with DataContract(IsReference = true) attribute and each member you want to expose with DataMember. In some cases it is also possible to mark only members you don't want to expose with IgnoreDataMember attribute but the first solution is better because it will avoid exceptions with circular references in case of serializing object graph with backward references.

    The first solution is automatically used by default entity framework generator and EntityObject Generator T4 template but it mark as DataMember every property. If you want to use T4 template and still selectively describe which property should be or should not be serialized you must modify T4 template to do it for you (in the most complex case you can end in modifying EDMX manually as well to use some structural annotations as an additional information for T4 template).

    Best regards,
    Ladislav

    Monday, August 29, 2011 6:51 AM

All replies

  • Hello,

    you must mark entity class with DataContract(IsReference = true) attribute and each member you want to expose with DataMember. In some cases it is also possible to mark only members you don't want to expose with IgnoreDataMember attribute but the first solution is better because it will avoid exceptions with circular references in case of serializing object graph with backward references.

    The first solution is automatically used by default entity framework generator and EntityObject Generator T4 template but it mark as DataMember every property. If you want to use T4 template and still selectively describe which property should be or should not be serialized you must modify T4 template to do it for you (in the most complex case you can end in modifying EDMX manually as well to use some structural annotations as an additional information for T4 template).

    Best regards,
    Ladislav

    Monday, August 29, 2011 6:51 AM
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?

    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, September 1, 2011 6:36 AM
    Moderator