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!