Adding the rating column to the sqlite database does not work as advertised
In order to add the new rating column to the database using sqlite on Mac, I had to do the following:
rm MvcMovie.db
dotnet ef migrations remove
dotnet ef migrations add InitialCreate
dotnet ef database update
In other words, simply deleting the MvcMovie.db file and running dotnet ef database update was not enough. It was required to remove the InitialCreate migration and then create it again first.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: cca95f5e-0fe4-bbd2-fd23-13562e02f888
- Version Independent ID: c3fb9673-4da6-5eeb-08bb-6f8e7bc8e0c6
- Content: Add a new field to an ASP.NET Core MVC app
- Content Source: aspnetcore/tutorials/first-mvc-app/new-field.md
- Product: aspnet-core
- Technology: aspnetcore-tutorials
- GitHub Login: @Rick-Anderson
- Microsoft Alias: riande
I don't think you need to remove the DB
~rm MvcMovie.db~
dotnet ef migrations remove
dotnet ef migrations add InitialCreate
dotnet ef database update
cc @tdykstra
@jfitzpatrick99 thanks for bringing this to our attention.
Thanks for the fix!
dotnet ef migrations remove is the step that is missing from the tutorial for those of us on mac.
That works if you only have 1 migration. If you happen to have more than one use
Delete migration folder.
dotnet ef database drop
dotnet ef migrations add InitialCreate
dotnet ef database update
Thanks, this resolved issue with the tutorial (and using Ubuntu).
Added all the validation rules and I'm getting a NULL error (SqlNullValueException: Data is Null. This method or property cannot be called on Null values). I assume it's because of the Rating column being a required field. I've updated the SeedData.cs file with ratings but that will only help if I start from scratch.
There are no instructions on how to update the database or data with EF. I've not dropped the database, because well I shouldn't have to - so what am I missing?
I was following the Visual Studio instructions - not VS Code.
Calling Update-Database didn't work for me here either. However, I believe I figured out why.
Remember that in our SeedData class we added the following block:
// Look for any movies.
if (context.Movie.Any())
{
return; // DB has been seeded
}
As such, because our database already contained data the seed method would just exit.
I ended up having to drop the database, delete all migrations, re-create the database with a new migration, and then restart IIS express in order to run the startup.cs as described in the previous tutorial in order to get a fresh set of data from the seed method.
Hope this helps!
@trunker76 wrote
delete all migrations
No need to do that.
As such, because our database already contained data the seed method would just exit.
The tutorial tells you that.
Thanks for the fix. Also thanks to Rick for making such vast and easy to understand docs. I wished I knew about EF core when I was in college.
Add-Migration nameOfMigration -Context testdbContext Update-Database -Context testdbContext
this is the solution
Issuing a dotnet build just before before issuing dotnet ef database update helped me. I was getting an error about m.Rating not existing.
Using VS (macOS): I ran through this tutorial twice and got stuck at the very end both times. Only deleting the DB file and rerunning dotnet ef database update did not work. The program would apply the initial migration w/o the Ratings column addition. When I tried to add a second migration to add the column, it also added added an ALTER command for the Price column (not sure why), so that would fail when trying to to update. I tried removing all migrations (dotnet ef migrations remove) but it would not let me remove the InitialCreate migration (The error I kept receiving was: "The migration '20200428201611_InitialCreate' has already been applied to the database. Revert it and try again. If the migration has been applied to other databases, consider reverting its changes using a new migration."). The only way to force the program to create a new Initial migration that included the Ratings column was to completely delete the db and the migrations folder. This extremely frustrating because no where in the tutorial does it say that migrations need deleted or rolled back (still have no idea how to remove the InitialCreate migration w/o brute force deleting it).
Thanks for contacting us. We believe that the question you've raised have been answered. If you still feel a need to continue the discussion, feel free to reopen it and add your comments.