简体   繁体   中英

Retrieving DateTime from SQL Server DB

I am using entity framework and storing a DateTime with kind as 'Utc'.

For unit testing puroposes when retrieving this DateTime from the database it is coming out with an 'unspecified' kind and different ticks value.

I understand that Entity Framework cannot automatically assign it the Utc kind but I cannot understand why it would come out with a different ticks value.

This is a problem since I am using Mspec to test the retrieved value against the one that was put in using

result.Date.ShouldEqual(retrievedDate)

This statement is returning false since the kinds on each value are different. I also tried:

DateTime.Compare(result.Date, retrievedDate)

but this returned false since the ticks value is different.

I have fixed the issue by using a DateTimeOffset property instead of a DateTime.

Microsoft state that:

DateTimeOffset should be considered the default date and time type for application development.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM