Timestamp data from DB2 is not accurate when using EntityFramework

烂漫一生 提交于 2019-12-13 05:51:56

问题


I have data in IBM DB2 and what I am trying to do is to get that data using EntityFramework. I have one column that has TIMESTAMP values in this format: dd.MM.yyyy hh:mm:ss.ffffff and it is primary key in that table.

When I am getting data from DB2 database time part of timestamp is lost. On the other side I have entity that has string property for that Id:

public string Id { get; set; }

This is a method that will return specific merchant. It is expected only one, because timestamps aka Ids are unique. But, when data gets pulled and when time part is lost I get 9 merchants with same Id and of course exception).

MerchantEntity IMerchantRepository.GetMerchantByRegistrationNumber(string companyRegistrationNumber)
{
     var db = merchantDataContextProvider.GetMerchantContext();
     var merchant = db.Merchants.Include(x => x.MerchantProperty).
         SingleOrDefault(x => x.MerchantProperty.CompanyRegistrationNumber == companyRegistrationNumber);
     return merchant;
}

I have tried to use DateTime type for Id, the only difference was that it was cutting last 3 numbers, instead whole time part.

Did anyone had same or similar problem? Who to get accurate data when using DB2?


回答1:


Timestamps as primary key.. not a great idea. If you do however want to use time as your basis for creating an ID of the merchant, you could do this:

var newId = string.format("{0:D19}", DateTime.Now.Ticks)

which will give you a 19-digit long number based on the current time. The problem of course is that if the clock on the PC resets, or you run the program on a different computer, different timezones etc, this strategy will fail.

If you need something that uniquely identifies a row in a table, use GUID's. They're very unique, and the chance of two different processes making the same one is very close to zero



来源:https://stackoverflow.com/questions/37298510/timestamp-data-from-db2-is-not-accurate-when-using-entityframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!