Open huzaynbolt opened 6 years ago
The DataMember attribute is used by, amongst others, ASP.Net, WCF and OWIN. I assume your project uses either or similar?
I can't answer your question since this is not related to MongoRepository but to some other part of your project like aforementioned ASP.Net/WCF/OWIN or such. But, generally, yes, you should add a DataMember attribute to all properties you want serialized.
No, I am actually using a WEB API does that relate to such framework you enlisted and might it be the cause. You also added the [DataMember] attribute to the Entity class on Id property, Why was that done
does that relate to such framework you enlisted
Yes
You also added the [DataMember] attribute to the Entity class on Id property, Why was that done
Because otherwise you won't be able to add it 😉
Because otherwise, you won't be able to add it
But if I am not working with the WCF,Web API ... frameworks would removing it still makes it work the way it should
But if I am not working with the WCF,Web API
Then it won't do anything.
removing it still makes it work the way it should
Why would you want to remove it? It doesn't hurt leaving it in there if that's what you mean?
Maybe you can explain a little of your actual problem; remember that as an outsider I have no clue on what you're doing and your initial issue report doesn't clarify much. I'm not psychic 😉
Thanks Rob, I am actually using the Repository as my DAL (as it should be), Then I created Proxy classes that inherited from the base Entity class (as stated in your example). Like the Author Class I use as an example above, whenever I try Adding an instance of the object into the repository it work as it should and all filed value are mapped to the mongo documents.
The ISSUE Whenever I try requesting the documents I added earlier via the enumerator in the MongoRepository, It only returns the Id not until I add a [DataMemebr] attribute on each field/property of the Author class
The ISSUE Whenever I try requesting the documents
... via Web API. That's pretty relevant information I think 😉 The quoted could also be read as: "Whenever I try requesting the documents ... from MongoDB using myRepo.Where(...)...
". The latter is a MongoRepository support question / issue, the former is an issue related to Web API (or other framework).
But as said; Web API requires the DataMember
attribute since the Entity
class is annotated with the DataContract
attribute:
If you prefer an "opt-in" approach, decorate the class with the DataContract attribute. If this attribute is present, members are ignored unless they have the DataMember. You can also use DataMember to serialize private members.
I resolved this challenge using CustomResolver. Please refer here for more details. https://refactorthat.wordpress.com/2014/01/18/getting-datacontract-atttribute-and-web-api-to-work-together/
The sample class I add earlier was
and the response on getting list of authors was
After adding DataMember to each property like below
I was able to get the name and dob as part of the response
Must I add the attribute [DataMember] to every field of my POCO classes before it could map to a field in the container document?