0

So I have a simple C# web API with some CRUD methods in a "checklistitem" controller. Basically a person can have a checklist with 0 - multiple checklist items in it, and a checklist item must belong to 1 person (via userId).

So I've seeded my DB to add 3 CLI (checklistitems) to me (the sole person in my app for now) with my userId (which is a GUID). I also use GUID for Id with the CLI's, so you don't have cli 1, 2, 3 etc but guid1, guid2, guid3 etc...

Anyway, after the seed, the items remain unchanged position in the list, first firstItem being 1, secondItem being 2, thirdItem being 3. When I add an item, I enter a title & description (e.g. in Postman) and then generate a new Guid in my repository pattern, right before adding it to my DBContext & saving changes.

When it adds the item, it seems to add randomly rather than at the end of the "stack" of items. So if I add "fourthItem" i want it to be the 4th item in the table, but instead it's added as position 2... When I keep adding items, it just randomly throws them in whatever order it seems to feel like...

Is this because I use GUIDs as in the table? Or something else? How can I fix this or add something to my code to make it work "properly"?

Here's my model:

public class ChecklistItem: IEntity
{
    [Key]
    public Guid Id { get; set; }

    [Required]
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsChecked { get; set; } = false;

    [Required]
    public Guid UserId { get; set; }
    public virtual User User { get; set; }
}  

My repo code (for Create):

public ChecklistItem Create(ChecklistItem o)
    {
        o.Id = Guid.NewGuid();
        var cli = _TCACtx.ChecklistItems.Add(o);
        _TCACtx.SaveChanges();
        return cli.Entity;
    }  

Seed of items:

modelBuilder.Entity<ChecklistItem>().HasData(
            new ChecklistItem() { Id = Guid.NewGuid(), Title = "My first item", Description = "this is my first item", IsChecked = false, UserId = new Guid("63EE02DA-4360-45AD-87C2-E6D36B6F50AA") },
            new ChecklistItem() { Id = Guid.NewGuid(), Title = "My second item", Description = "this is my second item", IsChecked = true, UserId = new Guid("63EE02DA-4360-45AD-87C2-E6D36B6F50AA") },
            new ChecklistItem() { Id = Guid.NewGuid(), Title = "My third item", Description = "this is my third item", IsChecked = false, UserId = new Guid("63EE02DA-4360-45AD-87C2-E6D36B6F50AA") }
            );  

(the userId guid links these items to my "user" model, which is me in the app).

If more code is needed, let me know, thanks!

EDIT: see image for my DB. Items were added like they say they are (first, second, third, fourth, fifth etc...). First 3 items were seeded! enter image description here

Nazim Kerimbekov
  • 4,712
  • 8
  • 34
  • 58
Tempuslight
  • 1,004
  • 2
  • 17
  • 34
  • Here's your answer https://stackoverflow.com/questions/34795577/what-logic-determines-the-insert-order-of-entity-framework-6 – Barry O'Kane Apr 16 '19 at 13:18
  • 1
    Repeat to yourself over and over - "Tables have no order". If you need some particular order it is a) Up to you to ensure sufficient data is stored in one or more columns to allow the order to be specified and b) to use an `ORDER BY` based on those column(s) during retrieval. – Damien_The_Unbeliever Apr 16 '19 at 13:22
  • You should not use a guid as a primary key in a database. See [this link](http://csharptest.net/1250/why-guid-primary-keys-are-a-databases-worst-nightmare/) (among many others) – Chris Dunaway Apr 16 '19 at 13:49
  • @ChrisDunaway that was extremely fun and informative to read, thanks! – Tempuslight May 08 '19 at 09:17

1 Answers1

2

They're not in random places, they in order by GUID. Since a GUID is essentially random (not exactly, but a discussion for another day), the order will appear random, but it's effectively sorted as if it's a string. If you want to show them in order that they are added, you'll need to use a one-up integer ID, or if you REALLY need to know when they were put it, not just visually see things that line up, use a date and sort on that.

gilliduck
  • 2,762
  • 2
  • 16
  • 32