使用EntityFramework时,DB2的Timestamp数据不准确 [英] Timestamp data from DB2 is not accurate when using EntityFramework
问题描述
我有一列具有以下格式的TIMESTAMP值:dd.MM.yyyy hh:mm:ss.ffffff,它是该表中的主键。
当我从DB2数据库获取数据时间戳的时间部分丢失。
另一方面,我有一个具有该字符串属性的实体:
public string Id {get;组; }
这是一种返回特定商家的方法。预计只有一个,因为名称Ids的时间戳是唯一的。但是,当数据被拉扯并且时间部分丢失时,我得到9个商家具有相同的Id,当然例外)。
商家实体IMerchantRepository.GetMerchantByRegistrationNumber(string companyRegistrationNumber)
{
var db = merchantDataContextProvider.GetMerchantContext();
var merchant = db.Merchants.Include(x => x.MerchantProperty)。
SingleOrDefault(x => x.MerchantProperty.CompanyRegistrationNumber == companyRegistrationNumber);
返回商家
}
我试图使用DateTime类型为Id,唯一的区别是它正在切割最后3个数字,而不是整个时间部分。
有没有人有相同或类似的问题?谁在使用DB2时获得准确的数据?
时间戳作为主要的关键..不是一个好主意。不过,如果您希望使用时间作为创建商家ID的依据,可以这样做:
var newId = string.format({0:D19},DateTime.Now.Ticks)
根据当前时间给您一个19位数的长号码。当然问题在于,如果PC上的时钟复位,或者在不同的计算机上运行程序,不同的时区等,这个策略就会失败。
如果您需要在表格中唯一标识行的内容,请使用GUID。他们是非常独特的,两个不同的过程使得同一个进程的机会非常接近零
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?
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
这篇关于使用EntityFramework时,DB2的Timestamp数据不准确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!