Timestamp data from DB2 is not accurate when using

2019-09-15 18:49发布

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条回答
劫难
2楼-- · 2019-09-15 19:44

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

查看更多
登录 后发表回答