I am confused about one thing that how and in which order should we execute the commands in project manager console when a new model and modify the existing model requires to update the database.
1) Enable-migrations
2) Add-Migration
3) Update-database
I am presuming that on the base of model changes or new model, we require these commands to be executed in the mentioned order each time. Kindly guide me in this regard. –
If you want to enable migration to your project for first time, run
PM> Enable-Migrations
If migration is enabled and you changed your model and want to affect changes to your database run
PM> Add-Migration [migration-name]
PM> Update-Database
There are more useful switches for above commands that you can find at EF Migrations Command Reference.
Related
I am just into dotnet and I am finding it difficult as most of the resource is available only for windows.
I have the docker running. When I try to update the database (dotnet ef database update) in the command terminal, it builds and shows an error saying "Connection string keyword 'server' is not supported."
The project is empty and its the default one after creating a new project with MVC
For incorporating Server in the visual studio I made few changes in the app settings.json
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=leavemanagementnet6;User=SA;Password=MyPassword123#;"
},
By default, it uses SQL Lite. Under program.cs, it should be changed to UseSqlServer(connectionString).
Also, the default migration has to be removed and created again. If its not done, we will be facing error while updating.
dotnet ef migrations remove
dotnet ef migrations add CreateIdentity -o Data/Migrations
I'm a newbie in entity framework. I have seen examples of using migrate.exe or enable-migration etc.
But migrate.exe does not exist any more. I explored the web and found that ef6.exe replaced the old migrate.exe
so I tried
ef6 -contexttypename musicstoredatacontext
ef6 -context musicstoredatacontext
nothing worked
I cannot find further support/documentation regarding the migration commands. and i'm new to migration.
Can somebody give me some hint how to work on it?
"If you execute Update-Database -Verbose inside Visual Studio's Package Manager Console, it will show you the call it uses to ef.exe to apply migrations." -Brice Lambson
It will be something like:
<PATH_TO_EXE>\ef6.exe database update --connection-string "<CONNECTION_STRING>" --connection-provider System.Data.SqlClient --verbose --no-color --prefix-output --assembly <PATH_TO_DLL> --project-dir <PATH_TO_PROJECT_DIR> --language C# --data-dir <PATH_TO_APP_DATA> --root-namespace <NAMESPACE> --config <PATH_TO_WEB_CONFIG>
Some of the arguments above can be omitted.
You can use the same strategy to determine how to create a migration (Add-Migration) with ef6.exe.
SOURCE: https://github.com/dotnet/ef6/issues/1365#issuecomment-540067758
use the command help to get the docs
ef6 --help
or
ef6.exe migrations --help
I succeeded in adding migration by updating powershell and use add-migration initial command
Reference can be found here.
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet
I'm using vagrant to host my DB and website, but I make changes to my project locally and push changes to the vagrant site. I can run migrate.exe to update the DB but I can't figure out how to run add-migration? If I run it locally, it says I have pending changes because I have no DB locally? Can anyone point me in the right direction?
One option to update database without use dotnet ef database update is execute the command bellow in some default action or startup routine
_dbContext.Database.Migrate();
I am not successful in adding ef core migrations in a separate project. I have provided the project structure at the end.
The DbContext class and the migrations are in separate projects such that the former is a class library and the migrations project is a console application. I added a reference to the DbContext project in the migrations project however, I am getting errors while running migration commands
\EF.BlogsDb.Migrations > dotnet ef migrations add InitialCreate --project EF.BlogsDb.Migrations.csproj
No DbContext was found in assembly 'EF.BlogsDb.Migrations'. Ensure that you're using the correct assembly and that the type is neither abstract nor generic.
I followed advise from this link but I am not sure of the following.
In which project should i add these lines? is it the migrations project or my web project or the DbContext project?
options.UseSqlServer(
connectionString,
x => x.MigrationsAssembly("MyApp.Migrations"));
Why should i Add a reference to the migrations assembly from the startup assembly?
From which location/folder/project should i run this command?
dotnet ef migrations add NewMigration --project MyApp.Migrations
I can share the source code if you can tell me how to do that.
Thanks in advance for the clarifications one might provide!
Here is my project structure looks like:
The issue is solved using the following command.
dotnet ef --startup-project ../Project.Api/ migrations add InitialModel
Here I am using separate folder for the api and the EF.
Run the above command from the EF project folder
Start up project means referred to the api
Since updating my solution to use ASPNET Identity instead of the old membership, the migrate.exe commando to update database schema stopped working. The only major change is that my Context now inherits from a IdentityDbContext, when before it inherited from a DbContext.
Everything works fine running update-database on package manager console, but using migrate.exe on the command line doesn't work anymore. I get the error:
System.Data.Entity.Migrations.Infrastructure.MigrationsException: No migrations configuration type was found in the assembly 'SampleProject.Repository.EF'. (In Visual Studio you can use the Enable-Migrations command from Package Manager Console to add a migrations configuration).
I have a Configuration.cs file for the migrations, as always had and when running the "enable-migrations" suggested I get a message saying that migration are already enabled for the project.
Does anyone knows what the problem might be?
Thanks
Just a quick update on the solution.
It was DLL version conflict when running migrate.exe.
I was using the output from my web project (which also contained DLL for the EF project).
I updated it in order to use the bin content of my EF project and it worked just fine.