简体   繁体   English

将数据上传到SQL表(实体框架)后,实体按时间错误的方式排序

[英]Entities are sort by time wrong way after uploading data to SQL table (Entity Framework)

I have a problem with Id order in my SQL table. 我的SQL表中的ID顺序有问题。 I created a program, that reads some variables from machine and then records these values into SQL table by means of Entity Framework. 我创建了一个程序,该程序从计算机读取一些变量,然后通过实体框架将这些值记录到SQL表中。 For uploading of values it uses two lists - when the first list is filled (time of filling, for example, is about 10 minutes) with variables, the second list (that contains recorded variables) uploads to SQL table and clears itself. 为了上载值,它使用两个列表-第一个列表用变量填充时(例如,填充时间约为10分钟),第二个列表(包含记录的变量)上载到SQL表并清除自身。 After 10 minutes lists change. 10分钟后,列表会更改。

It works fine, but once I noticed that some rows (some = 100 rows from 10000 rows) have "broken order". 它工作正常,但是一旦我注意到某些行(某些= 10000行中的100行)具有“中断顺序”。 I mean there is something like below: 我的意思是下面这样:

ID - Value - Time ID-值-时间

1000 - 3 - 15:00 1000-35:00

1001 - 4 - 15:01 1001-4-15:01

1002 - 1 - 14:58 !! 1002-1-14:58 !!

1003 - 2 - 14:59 !! 1003-2-14:59 !!

1004 - 5 - 15:02 1004-5-15:02

1005 - 6 - 15:03 1005-6-15:03

In other words there are some variables that sometimes change their order in SQL table. 换句话说,有些变量有时会更改其在SQL表中的顺序。 Thousands rows before and after these problem rows are OK. 这些问题行之前和之后的数千行都可以。

UPD: As I just find out, this problem occurs every 50 th and 51 th minutes if time (14:50:xx...15:50)... very strange for understanding... UPD:我刚刚发现,如果时间(14:50:xx ... 15:50),此问题每5051分钟出现一次。

So I tried to edit code a little (don´t throw bricks at me - I am quite new in programming). 因此,我尝试了一些编辑代码(不要对我产生砖头-我在编程方面是个新手)。 There is a method that writes recorded variables from lists to SQL table: 有一种方法可以将记录的变量从列表写入SQL表:

public void WriteRecordedVariableToSQL()
{
    if (timeTumbler == true && VariablesToBulkSQLList2.Count > 0)
    {
        List<VariableRecord> sortedVariablesToBulkSQLList2 = VariablesToBulkSQLList2.OrderBy(o => o.Time).ToList();
        context.VariableRecords.AddRange(sortedVariablesToBulkSQLList2);
        context.BulkSaveChanges();
        VariablesToBulkSQLList2.Clear();
        sortedVariablesToBulkSQLList2.Clear();

    }

    if (timeTumbler == false && VariablesToBulkSQLList1.Count > 0)
    {
        List<VariableRecord> sortedVariablesToBulkSQLList1 = VariablesToBulkSQLList1.OrderBy(o => o.Time).ToList(); //sortuju podle casu
        context.VariableRecords.AddRange(sortedVariablesToBulkSQLList1);
        context.BulkSaveChanges();
        VariablesToBulkSQLList1.Clear();
        sortedVariablesToBulkSQLList1.Clear();

    }

}

But the result was the same. 但是结果是一样的。 As you see, I use Entity FrameWork Extentions( EFE ) and their AddRange for adding the entire lists. 如您所见,我使用Entity FrameWork Extensions( EFE )及其AddRange来添加整个列表。 May the problem be there? 问题可能在那里吗?

So I´d like to fix this problem that there are values ordered by time in SQL table (with right order of ID). 因此,我想解决此问题,即SQL表中的值按时间排序(ID的顺序正确 )。

Can you help me? 你能帮助我吗? Thanks in advance. 提前致谢。 Hope you understand my poor English. 希望你能理解我的英语不好。

Here's a little on the basics. 这里有一些基本知识。 Id in your case (a generated PK by SQL Server) is a meaningless field. 在您的情况下,ID(SQL Server生成的PK)是没有意义的字段。 In other words, it is a field that conveys no useful information beyond being a unique key for that particular row in the table. 换句话说,这是一个字段,除了作为表中该特定行的唯一键之外,没有传达任何有用的信息。 It does not specify order or sequence. 它不指定顺序或顺序。

For your example, sorting is part of the data retrieval and would involve selecting (and sorting) based upon the field you use to store the time. 在您的示例中,排序是数据检索的一部分,可能需要根据您用于存储时间的字段进行选择(和排序)。 It does not affect or influence the primary key. 它不影响或不影响主键。

Add-on: It will hurt you in the long run to attempt to "build in" intelligence with your primary keys. 附加组件:从长远来看,尝试使用主键“构建”智能会伤害您。 That is one reason why they are also referred to as "surrogate" keys. 这就是为什么它们也被称为“代理”键的原因之一。 It is a piece of artificial information that is unchanging and refers to a unique row. 它是一条不变的人造信息,是指唯一的一行。

Other key types include "candidate" keys (unique but changeable) and "foreign" keys (primary key to another entity). 其他键类型包括“候选”键(唯一但可以更改)和“外来”键(另一个实体的主键)。 Indices which are non-essential but useful for performance include "compound" keys which combine multiple fields (as a candidate key or for ease of sorting) and single field indices which are mostly for DB performance. 无关紧要但对性能有用的索引包括组合多个字段(作为候选关键字或便于排序)的“复合”键和主要用于数据库性能的单个字段索引。

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

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