0

I have a problem with Id order in my SQL table. I created a program, that reads some variables from machine and then records these values into SQL table by means of Entity Framework. 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. After 10 minutes lists change.

It works fine, but once I noticed that some rows (some = 100 rows from 10000 rows) have "broken order". I mean there is something like below:

ID - Value - Time

1000 - 3 - 15:00

1001 - 4 - 15:01

1002 - 1 - 14:58 !!

1003 - 2 - 14:59 !!

1004 - 5 - 15:02

1005 - 6 - 15:03

In other words there are some variables that sometimes change their order in SQL table. Thousands rows before and after these problem rows are OK.

UPD: As I just find out, this problem occurs every 50th and 51th minutes if time (14:50:xx...15:50)... very strange for understanding...

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:

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. 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).

Can you help me? Thanks in advance. Hope you understand my poor English.

2
  • Which type has the Time property you are using for sorting (VariablesToBulkSQLList2.OrderBy(o => o.Time))? Is it a TimeSpan? A DateTime? Aug 9, 2018 at 15:39
  • @GeorgPatscheider it is actually a DateTime Aug 10, 2018 at 13:29

1 Answer 1

1

Here's a little on the basics. Id in your case (a generated PK by SQL Server) is a meaningless field. 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.

3
  • TL;DR; Don't sweat it, not worth fixing
    – John White
    Aug 9, 2018 at 17:35
  • You are quite welcome. You said "new" to programming and on your path, you'll trip over the basics like the rest of us on more than one occasion. So, always a good idea to go back and check the basic assumptions first. My favorite story is my small client with server crashes every night. I stayed in the office one night, cleaning crew unplugged the server to plug in a vacuum... boom, server outage.
    – John White
    Aug 10, 2018 at 14:20
  • these type of problem seems like very unpredictable! so, if you are still interesting in my start question above, I found the solution - instead of combination AddRange(list) and BulkSaveChanges() I tried BulkInsert(list) - and it works fine and with right order! But, I agree with you - column Id with PK is meaningless for me, it only shows me unique number of entity. Aug 14, 2018 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.