AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Adding the rating column to the sqlite database does not work as advertised

Open jfitzpatrick99 opened this issue 7 years ago • 14 comments

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.

jfitzpatrick99 avatar Jun 25 '19 17:06 jfitzpatrick99

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

Rick-Anderson avatar Jun 26 '19 15:06 Rick-Anderson

cc @tdykstra

Rick-Anderson avatar Jun 26 '19 15:06 Rick-Anderson

@jfitzpatrick99 thanks for bringing this to our attention.

Rick-Anderson avatar Jun 26 '19 15:06 Rick-Anderson

Thanks for the fix!

dotnet ef migrations remove is the step that is missing from the tutorial for those of us on mac.

SamEakin avatar Jul 24 '19 16:07 SamEakin

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

Rick-Anderson avatar Jul 24 '19 16:07 Rick-Anderson

Thanks, this resolved issue with the tutorial (and using Ubuntu).

jueefo avatar Sep 23 '19 10:09 jueefo

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.

armydee avatar Sep 24 '19 10:09 armydee

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 avatar Nov 04 '19 02:11 trunker76

@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.

Rick-Anderson avatar Nov 04 '19 04:11 Rick-Anderson

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.

LayersOfAbstraction avatar Dec 17 '19 01:12 LayersOfAbstraction

Add-Migration nameOfMigration -Context testdbContext Update-Database -Context testdbContext

this is the solution

max6811 avatar Dec 17 '19 21:12 max6811

Issuing a dotnet build just before before issuing dotnet ef database update helped me. I was getting an error about m.Rating not existing.

mannym2124 avatar Jan 31 '20 04:01 mannym2124

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).

trevorbenyack avatar Apr 29 '20 01:04 trevorbenyack

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.

Rick-Anderson avatar May 09 '20 01:05 Rick-Anderson