简体   繁体   English

使用EntityFramework时来自DB2的时间戳数据不准确

[英]Timestamp data from DB2 is not accurate when using EntityFramework

I have data in IBM DB2 and what I am trying to do is to get that data using EntityFramework. 我在IBM DB2中有数据,而我想做的就是使用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. 我有一列具有以下格式的TIMESTAMP值:dd.MM.yyyy hh:mm:ss.ffffff,它是该表中的主键。

When I am getting data from DB2 database time part of timestamp is lost. 当我从DB2数据库获取数据时,时间戳的一部分丢失了。 On the other side I have entity that has string property for that Id: 另一方面,我有一个具有该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. 由于时间戳(也称为Id)是唯一的,因此只能预期一个。 But, when data gets pulled and when time part is lost I get 9 merchants with same Id and of course exception). 但是,当数据被提取并且时间部分丢失时,我得到9个具有相同ID的商人,当然还有例外)。

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. 我尝试将DateTime类型用于Id,唯一的区别是它减少了最后3个数字,而不是整个时间部分。

Did anyone had same or similar problem? 有没有人遇到过相同或相似的问题? Who to get accurate data when using DB2? 使用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: 但是,如果您确实希望使用时间作为创建商家ID的基础,则可以执行以下操作:

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

which will give you a 19-digit long number based on the current time. 会根据当前时间为您提供一个19位长的数字。 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. 当然,问题在于如果PC上的时钟重置,或者您在其他计算机,不同时区等上运行该程序,则该策略将失败。

If you need something that uniquely identifies a row in a table, use GUID's. 如果您需要某些东西可以唯一地标识表中的一行,请使用GUID。 They're very unique, and the chance of two different processes making the same one is very close to zero 它们非常独特,两个不同的流程制造同一个流程的机会非常接近零

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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