I\'ve used yoman to generate an ASP.Net Core Web API application via the Visual Studio Code Editor. For reference, I followed this tutorial here
The API works fine. Ho
The correct format to add a new migration is dotnet ef migrations add yourMigrationName
and to update database is dotnet ef database update
Step 1
First we need to add reference in *.csproj file in the following way
Step 2 in Bash/Command propt
dotnet restore
Step 3
dotnet ef migrations add MyDbInitialMigration
You need to add:
dotnet tool install --global dotnet-ef
In this case, the project must be checked first. Migration cannot be initiated if there is any error in the project.
Im working on Mac, so Ruby is installed by default. My EF commands required lots of extra parameters --project
, --startup-project
etc. This was a pain to type every time, so I used rake to make this easier.
In my project root, I added a file called rakefile
with these contents:
desc "Add Migraion"
task :'add-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end
desc "Remove Migraion"
task :'remove-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
desc "Update Database"
task :'update-database' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
Then at the command line, I run these commands:
rake add-migration <migrationName>
rake remove-migration
rake update-database