Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preventing Entity Framework from serializing EntityKey, EntitySetName, etc

I have a webservice that retuns entity framework objects. On the calling side, I don't do anything but read the information, so I don't need these entity framework serialization attributes. Is it possible to remove the following serialized text when returning entity framework objects?

<EntityKey> <EntitySetName>ClassName</EntitySetName> <EntityContainerName>MyEntities</EntityContainerName> -<EntityKeyValues> -<EntityKeyMember> <Key>ClassID</Key> <Value xsi:type="xsd:int">9903</Value> </EntityKeyMember> </EntityKeyValues> </EntityKey>
like image 810
Josh Avatar asked Feb 24 '23 11:02

Josh


1 Answers

I've read up on how EF would require me to make a bunch of POCOs and such, which required me to do a lot of work. I decided to think about it some more and did some digging on the Entity object. After seeing that it wasn't sealed, I created a custom class that inherits from this class and added a 'new' statement to the EntityKey field with the XmlIgnore attribute. Going back to my EF designer class, I changed all classes to inherit from CustomEntityObject instead and voila!, no unneccessary fields. Whether this is future proof, I doubt it. But for this project, it works great and it didn't require me to write DTOs for all my classes. Hopefully someone else finds it useful:

public class CustomEntityObject : EntityObject {
    [DataMember]
    [Browsable(false)]
    [XmlIgnore]
    public new EntityKey EntityKey { get; set; }
}
like image 193
Josh Avatar answered May 09 '23 23:05

Josh