Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework and the XmlIgnoreAttribute

Say you have a one to one relationship in your entity model. The code generator will decorate it with the following attributes:

[global::System.Xml.Serialization.XmlIgnoreAttribute()]
[global::System.Xml.Serialization.SoapIgnoreAttribute()]
public RelatedObject Relationship { get {...} set {...} }

I want to serialize my parent object together with all its properties for which data has been loaded through an XML web service. Obviously, these related properties do not get serialized because of these attributes.

So for my purposes I just want to remove these "don't serialize me" attributes. I can do a find and replace in the designer code, but any modifications I make in the designer will put these attributes back in.

In my query I'm .Include()ing and explicitly loading only the child objects that I require for serialization. So I will make sure there are no circularities in my query. Some of the child properties are not required, so I won't Include() them, so they won't be serialized.

Else how do I achieve what I want to do? Make a separate call from my application for each child object? Say I'm returning hundreds of parent objects; I'd have to make hundreds of separate calls to get each child too.

How do I permanently get rid of these attributes?

VS 2008 / EF 3.5.

like image 452
Mike Chamberlain Avatar asked Feb 27 '23 09:02

Mike Chamberlain


1 Answers

Just don't do it. It's that simple.

You state on your post that you want to serialize the parent of your object, right?

Now let's see what happens when you do something like that...

  1. The serializer starts converting your object and it's properties
  2. When it finds the parent of your object, it starts serializing it
  3. While serializing the parent if finds the child object that it was serializing and goes back to 1.

And it will never get out, without some encouragement.

So those attributes are there for a good reason.

like image 61
Paulo Santos Avatar answered Mar 16 '23 22:03

Paulo Santos