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
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 am adding Data Model (Database First Approach) in .NET Core Class Library using Entity FrameWorkCore.
I have added all necessary packages and when I try to create the model using below command using PMC I am receiving below message:
"No project was found. Change the current working directory or use the --project option."
Command in PMC (using Core Class library):
dotnet ef dbcontext Scaffold "Server=XXX;Database=EFCore;User Id=XX;Password=XX;" Microsoft.EntityFrameworkCore.SqlServer -o Models
Try this example:
Scaffold-DbContext "DataSource=D:\Database\SQLite\Example.sqlite" Microsoft.EntityFrameworkCore.Sqlite -OutputDir Model
Tutorial
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 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");
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: