Задайте вопросЗадайте вопрос
 

ОтвеченоInsert order problem when there is no deterministic order, help

  • 20 мая 2009 г. 18:34Federico Silberberg Медали пользователяМедали пользователяМедали пользователяМедали пользователяМедали пользователя
     
    Hello everyone,

    I have this table in my DB:

    Person
    PersonID uniqueidentifier PK
    Name      varchar(100)
    ID           int IDENTITY (1,1)


    My problem is that I do the following:

    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "1st"});
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "2nd"});
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "3rd"});
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "4th"});
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "5th"});
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "6th"});
    context.SaveChanges();


    ID column of Person entity is Auto Increment in database.
    I would expect to have this result (taken for granted that table is brand new)...
    ID | Name
    ==========
    1  | 1st
    2  | 2nd
    3  | 3rd
    4  | 4th
    5  | 5th
    6  | 6th


    However I get this!...
    ID | Name
    ==========
    1  | 3rd
    2  | 2nd
    3  | 1st
    4  | 6th
    5  | 5th
    6  | 4th


    So after reading this: http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/d8448144-05dd-4c34-b93c-9336c3b62f6e/

    I tried with Colin Meek's advice:

    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "1st"}, ID=1);
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "2nd"}, ID=2);

    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "3rd"}, ID=3);
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "4th"}, ID=4);
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "5th"}, ID=5);
    context.Add(New Person { PersonID=Guid.NewGuid(), Name = "6th"}, ID=6);
    context.SaveChanges();


    However I get this!...
    ID | Name
    ==========
    1  | 1st
    2  | 2nd
    3  | 6th
    4  | 3th
    5  | 5th
    6  | 4th


    Why is not working? What is going on? Is there anyway I can get this to insert in the right order?

    Thanks

Ответы

Все ответы