Trouble with "dotnet run seeddata" when trying to seed a database with EntityFrameworkCore. Specifically with ASP.NET Core 6 Web API

29 views Asked by At

I have been following along with Teddy Smith's ASP.NET Core 6 Web API (although I am using .NET 8.0 since the video is a little old) and PokemonReviewApp is the name of the project (some might be familiar with it). I have come to the part where I have been trying to properly install Entity Framework and get the DbContext seeding into a database. Please help.

Using ASP.NET Core 6 Web API in Visual Studio 2022. I have created a bunch of models, defined a connection string to a SQL Server database on my machine and have that reflected in the Appsettings.json. I also thought I had all the correct frameworks installed via the NuGet Package Manager Console (Microsoft.EntityFrameworkCore.SqlServer, Design, etc etc.)

The 1st time I had this, it seeded the database inside SQL correctly. I was able to select the top 1000 rows from each table that was pulled in and saw the data for each row. That information was in the Seed.cs file, and it was copy/pasted from Teddy Smith's github for this specific project. These were the instructions in the video.

Although that part was successful, I was never able to get the SwaggerUI the pop open in the browser when I would debug the solution. This was my only problem. I troubleshooted for a while, gave up..and decided to start from scratch. I deleted the project solution and the database inside Microsoft SQL Server.

Now after going through the program again, I am getting errors when using the Terminal in VS2022 for dotnet run seeddata.

By the point, I have already used Package Manager Console to run Add-Migration InitialCreate. That command runs successfully; I then do Update-Database and that was successful, too. However when I open the terminal in VS2022, and type dotnet run seedddata, it doesn't work.

The error is long, but this is part of it :

fail: Microsoft.EntityFrameworkCore.Update[10000]
An exception occurred in the database while saving changes for context type 'PokemonReviewApp.Data.DataContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot insert the value NULL into column 'Name', table 'pokemonreview.dbo.Owners'; column does not allow nulls. UPDATE fails.

Any and all help would be appreciated; apologies for not explaining it well.

I was hoping to get the SwaggerUI to pop open in the browser when I would debug the project solution. (PokemonReviewApp ASP.NET Core Web API)

0

There are 0 answers