[CSHARP-2537] BsonDefaultValue attributes in properties is not working Created: 26/Feb/19  Updated: 25/Jun/20  Resolved: 25/Jun/20

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: Lawrence Assignee: Wan Bachtiar
Resolution: Done Votes: 1
Labels: question, rfw
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: JPEG File BusinessModel.JPG     JPEG File DataLayer.JPG    

 Description   

I tried added the BsonDefaultValue in datetime property, when I pass the that value it should take the value if it is null then it should take the default value of current datetime. It is not working for datetime even it's not working for string value also. Please help me on this.



 Comments   
Comment by Rachelle Palmer [ 25/Jun/20 ]

Hi there, thank you for reaching out to MongoDB. As this sounds more like a support issue, I wanted to give you some resources to get this question answered more quickly:

  • our MongoDB support portal, located at support.mongodb.com
  • our MongoDB community forums, located here
  • If you are an Atlas customer, there is free support offered 24/7 in the lower right hand corner of the UI.

Thank you!
Rachelle

Comment by David Berman [ 24/May/20 ]

If you have a document already in your database and you want to add a datetime field to your class, you need to use BsonDefaultValue or your serializer will throw an exception loading the existing document since the datetime field is missing from the document.  However, BsonDefaultValue doesn't accept a datetime object, so this is a very painful problem.

Comment by Lawrence [ 27/Feb/19 ]

Please find the below screenshot, I am using WebAPI in C# code to connect MongoDB. I declare the properties for BusinessModel and DataLayer. 

 

 

Comment by Jeremy Mikola [ 26/Feb/19 ]

lawce: Can you clarify which driver you're using? There is no reference to "BsonDefaultValue" or a "datetime" property in the mongo-c-driver and mongo-cxx-driver libraries, as far as I can tell. Additionally, it'd be helpful to include a snippet of code that demonstrates the problem you're seeing (even if this issue ultimately needs to be moved to another project).

Generated at Wed Feb 07 21:42:50 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.