Remove all attributes from DataContract and DataMember.

c# entity-framework

Question

I locate the[DataContract] and [DataMember] attributes are a little disorganized, so I'd like to accomplish this in code, maybe via a config function. Can this be done?

1
15
8/30/2012 11:09:47 AM

Accepted Answer

These qualities are not required at all.DataContractSerializer will serialize all public properties with getter and setter, however if you serialize entities with navigation properties, "cyclic reference" will quickly lead to an exception.

You have two options to prevent that exception:[DataContract(IsReference = true)] using your entity classDataMember if you want to serilize orIgnoreDataMember You don't want to serialize on all and all properties.

The last and most difficult alternative is to totally ignore characteristics and create them using custom classes.IDataContractSurrogate to regulate outside-of-the-type serialization.

You may create an entirely unique serialization technique, employ binary or XML serialization with all of its requirements, or both.

16
9/1/2011 7:03:34 AM

Popular Answer

No, the DataContractSerializer requires that you explicitly tell it what you want included; it is an opt-in serializer.

When using another serializer, you must use tools like NonSerializedAttribute or XmlIgnoreAttribute to instruct it to ignore certain elements.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow