विफल रहा है मैं एक मोंगोडीबी डेटाबेस के साथ काम कर रहा हूं। मुझे पता है कि जब आप मोंगो में डेटटाइम डालते हैं, तो यह इसे यूटीसी में बदल देता है। लेकिन मैं एक यूनिट परीक्षण कर रहा हूं, और मेरा दावा विफल रहा है।डेटटाइम ToLocalTime
[TestMethod]
public void MongoDateConversion() {
DateTime beforeInsert = DateTime.Now;
DateTime afterInsert;
Car entity = new Car {
Name = "Putt putt",
LastTimestamp = beforeInsert
};
// insert 'entity'
// update 'entity' from the database
afterInsert = entity.LastTimestamp.ToLocalTime();
Assert.AreEqual(beforeInsert, afterInsert); // fails here
}
मुझे कुछ स्पष्ट याद आना चाहिए। जब मैं डीबगर में लग रहे हैं, मुझे लगता है कि दिनांक के मैच देख सकते हैं, लेकिन ज़ोर अभी भी कहते हैं वे नहीं है (लेकिन वे करते हैं):
Result Message: Assert.AreEqual failed. Expected:<5/21/2015 8:27:04 PM>. Actual:<5/21/2015 8:27:04 PM>.
कोई भी विचार क्या मैं गलत यहाँ कर रहा हूँ?
संपादित करें:
:मैं दो संभव समाधान, जो दोनों के लिए मुझे कुछ करने के लिए याद करने के लिए आवश्यकता होती है (जो हमेशा पर भरोसा करने की सबसे अच्छी बात नहीं है ...) लेकर आए हैं
एक किसी भी दिनांक समय डेटाबेस से बाहर आने को काटना एक विस्तार विधि का उपयोग करने के लिए है: http://alexmg.com/datetime-precision-with-mongodb-and-the-c-driver/ पढ़ने के बाद,
public static DateTime Truncate(this DateTime dateTime) {
var timeSpan = TimeSpan.FromMilliseconds(1);
var ticks = -(dateTime.Ticks % timeSpan.Ticks);
return dateTime.AddTicks(ticks);
}
अन्य, POCO वर्ग में किसी भी दिनांक समय टैग करने के लिए है:
public class Car : IEntity {
public Guid Id { get; set; }
[BsonDateTimeOptions(Representation = BsonType.Document)]
public DateTime LastTimestamp { get; set; }
}
इस लेख MongoDB datetime परिशुद्धता समझा देखें।com/datetime-precision-with-mongodb-and-the-c-driver/ –