Why does my ASP.NET Core seed method only add sample data when the admin user does not exist?

2 weeks ago 20
ARTICLE AD BOX

Your seed data is either missing some information or and it needs to have some starters for example like this :

if (!db.Items.Any()) { db.Items.AddRange( new Item { Name = "Wireless Mouse", Description = "Basic wireless mouse for everyday use.", Price = 14.99m, StockQuantity = 40, Category = "Electronics", IsActive = true }, new Item { Name = "USB Keyboard", Description = "Standard wired keyboard.", Price = 19.99m, StockQuantity = 25, Category = "Electronics", IsActive = true }, new Item { Name = "Laptop Stand", Description = "Adjustable stand for laptops and tablets.", Price = 24.99m, StockQuantity = 15, Category = "Accessories" IsActive = true } ); }

Larry Wheels's user avatar

New contributor

Larry Wheels is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

2026-04-12T18:30:30.537Z+00:00

Ok can i see you program.cs ??

Also make sure u have this at the end of program.cs

using (var scope = app.Services.CreateScope()) { var services = scope.ServiceProvider; await .Data.SeedData.InitializeAsync(services); } And also make sure that in your essentials is like this : public class EssentialController : Controller { private readonly ApplicationDbContext _context; public EssentialController(ApplicationDbContext context) { _context = context; } public async Task<IActionResult> Index(string searchString) { var items = _context.Items.Where(i => i.IsActive); if (!string.IsNullOrWhiteSpace(searchString)) { items = items.Where(i => i.Name.Contains(searchString) || i.Description.Contains(searchString) || i.Category.Contains(searchString)); } return View(await items.ToListAsync()); }

Rebecca Adami's user avatar

New contributor

Rebecca Adami is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

2026-04-12T23:41:18.26Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article