I have three projects in my solution.
BaseApp.Data - which holds my DBContext.
BaseApp.Data.ProjectMigration - which holds all migration files (depends on BaseApp.Data)
BaseApp.Web - web application with Startup class.
I added my first migration like this:
dnx ef migrations add Initial -p BaseApp.Data.ProjectMigration
After this command the Migrations folder appears with all corresponding files in BaseApp.Data.ProjectMigration. So everything is ok so far.
Than I try to apply migrations:
dnx ef database update
After this command the database is created, but no migrations applied. I also tried
dnx ef database update -p BaseApp.Data.ProjectMigration
with same result. Also next command returns "No migrations were found".
dnx ef migrations list -p BaseApp.Data.ProjectMigration
Also if I add migrations to BaseApp.Data then everything works fine. The next commands work as expected:
dnx ef migrations add Initial -p BaseApp.Data
dnx ef database update
So is it possible to hold migration files not in DBContext project?
You'll also need to set your migrations assembly.
options.UseSqlServer(connectionString)
.MigrationsAssembly("BaseApp.Data.ProjectMigration");
Related
I'm developing a project on Mac. The solution has multiple projects with in it, Domain, WebPortal and API.
From within the WebPortal project I've created a migration by running the following command from within that project;
dotnet ef migrations add ApplicationUser_Deactivate -o ../SolutionName.Domain/data/migrations
This has created the migration files within the Domain project.
The problem is, running the command dotnet ef database update doesn't appear to pickup the migration files from that location and I can't seem to find a way to tell the command where the migration files are.
Is there a way to do this?
Please try
dotnet ef database update --project .SolutionName.Domain/data/migrations or
dotnet ef database update --p SolutionName.Domain
Argument p, project - Relative path to the project folder of the target project. Default value is the current folder.
I have a project I have been working on in ASP.NET Core MVC, and now that I have published it:
I want to be able to force all migrations onto the database active on our server (my local database is just a copy so naming and table names and etc are the same).
Once I copy across the files to the server folder and run it, currently none of the migrations update the database and I get missing columns and tables and etc errors.
I have tried using cmd on the published file with
dotnet AppName.dll database update
as many people have suggested, but this didn't work at all. It doesn't seem to read the command at all.
Can someone please give me one straight answer? I have been struggling to find an answer that doesn't explain what to do or has old no longer working methods.
You can use
app.UseDatabaseErrorPage();
in your StartUp.Configure.
This will display the Apply Migrations Page you know from Developing.
Another way would be to create a migration script in development and use it against the production database. You can create an idempotent script with cmd while being in the Project Directory (where the StartUp.cs is located).
dotnet ef migrations script --output "migration.sql" --idempotent
This will create a script (a new file migration.sql) that will take any database that is on a prior level in the migration history to the current level.
You might generate a script of your migrations and install the script on your deployment environment. The advantage is you can store the migration scripts for example in a folder and keep track of what has been installed or what needs to be installed in the next release.
A migration script is generated with command Script-Migration:
Script-Migration -From <PreviousMigration> -To <LastMigration>
Be sure to check the EF Documentation (Entity Framework Core tools reference) before you run the script.
I have NET Core 3.1 application where my [models,dbcontext,application] are all in their own assemblies.I am trying to issue a migration(s) for the application.I keep getting this error:
$ dotnet ef migrations add InitialCreate --project DataAccess
MSBUILD : error MSB1009: Project file does not exist. Switch:
DataAccess Unable to retrieve project metadata. Ensure it's an
MSBuild-based .NET Core project. If you're using custom
BaseIntermediateOutputPath or MSBuildProjectExtensionsPath values, Use
the --msbuildprojectextensionspath option.
Current Structure
*root
-App (.NET Core 3.1)
-DataAccess (.NET Standard 2.1) (Contains the DBContext)
-Models (.NET Standard 2.1) (contains models)
I have also tried creating a separate assembly for migrations and use the MigrationAssembly extension in my App :
services.AddDbContext<[some DBContext]>(x => x.UseSqlServer([some string],x=>x.MigrationsAssembly("Migrations")));
Tried Structure
*root
-App
-Migrations (.NET Standard 2.1)
-DataAccess (.NET Standard 2.1)
-Models (.NET Standard 2.1)
I do not understand how this should be done.I want to be able to do migrations and ideally i would like to keep them in their own assembly.Currently i can't do them at all.
P.S I have also tried adding this to my App csproj file:
<GenerateRuntimeConfigurationFiles>True</GenerateRuntimeConfigurationFiles>
the answer given by #Ajay above finally worked for me, after days and days googling for it:
to insert the migrations assembly complete path in the --project argument, like this:
dotnet ef migrations add Mymigration --project "c:\repos\myproject\Data"
...and you should execute this command from your main startup project (like c:\repos\myproject\Web)
exactly same issue here, since hours, good to know not being the only one getting crazy about!
Finally worked it out:
I'm using EFCore.Tools in Package Manager Console instead of dotnet Cli:
//Add Migration
Add-Migration *name* -Project *Library project* -StartupProject *StartUp/Web project*
//Update Database
Update-Database -Project *Library project* -StartupProject *StartUp/Web project* -verbose
i would go this direction... have fun!
First change directory to project directory, then add .csproj
$ dotnet ef migrations add InitialCreate --project DataAccess.csproj
I hope it works!
I used the full Directory path and it's worked. And also we should use correct project references.
ex: dotnet ef migrations add Initial --project D:\NK-Pro\API\TaskAPI.DataAccess\TaskAPI.DataAccess.csproj
I'm developing a project using asp.net core, Entity framework code first, am trying to run EF migrations as a release step, i was advised to use migrate.exe, yet as far as i knew ef asp.net core doesn't generate migrate.exe file.
Am currently using the below command in the release step. After adding the ef core project's bin folder to the generated artifacts.
Command:
dotnet exec
--runtimeconfig ./HOST.runtimeconfig.json
--depsfile ./HOST.deps.json Microsoft.EntityFrameworkCore.Design.dll
--assembly ./DB_CONTEXT_DLL.dll
--startup-assembly ./HOST.dll --data-dir ./
--root-namespace DB_CONTEXT_NAMESPACE
--verbose database update --context DB_CONTEXT_CLASS -e development
The command step fails with the error:
A fatal error was encountered. The library 'hostpolicy.dll' required to execute the application was not found in 'd:\a\r1\a{Publish articats folder}\drop\EF\bin\release\netcoreapp1.1\'.
Am not sure what is the hostpolicy.dll and how to generate it as it isn't generated in the bin folder for the EF core project
Any advice?
You can do Entity framework migration during the deploy process. Simple steps:
Create a publish profile (e.g. Web Deploy package, right click project=>Publish)
General deploy package through Visual Studio Build task with /p:DeployOnBuild=true /p:PublishProfile=[profilename];DesktopBuildPackageLocation="$(build.artifactstagingdirectory)\WebAppCoreEF.zip" arguments
Add deploy task (e.g. Azure App Service Deploy) to deploy this package (You can do it in release)
I am working on an project and tried know to activate the migration but when I open the Nugget package console and enter “enable-migrations myProjectName” I get the massage “The term 'Enable-Migrations' is not recognized as the name of a cmdlet”. First I thought that I have to add an extra line in my project.json under commands, so I added “"ef": "EntityFramework.Commands" but that didn’t do the trick.”
Would be grateful for any pointers.
You did mention you are using the project.json file making me believe you are using the default ASP.NET 5 template which uses EF7 .
You can verify this by looking in your project.json at the version number.
Mine for example is:
"EntityFramework.Commands": "7.0.0-beta8",
"EntityFramework.SqlServer": "7.0.0-beta8",
or in your Package Manager Console type in:
get-command -module entityframework
which will give you the available commands you can use. From the above you can see I am using Entity Framework 7.
Enable-Migrations is not listed in the commands and that is the reason you are getting the error:
The term 'Enable-Migrations' is not recognized as the name of a cmdlet
If you wish to rather use EF6 instead you would have to remove those entries in your project.json and you can add via nuget or the package manager console EF6. Just keep in mind if you use EF6 the ASP.NET 5 Identity 3 will break as it is dependent on EF7.
The command Enable-Migrations does not exist in Entity Framework 7 like it did in previous Entity framework versions.
In EF7 the task of creating the migrations folder is now combined with the Add-Migration command.
See below for the Add-Migration command differences between EF6 and EF7: