The term 'scaffold-dbcontext' is not recognized as the name of a cmdlet, function, script file, or operable program (Entity Framework) - entity-framework-6

I am trying to use database-first model to create a dbContext and database entities.
When I run this command
scaffold-dbcontext "Data Source=(local);Initial Catalog=MyDb;Integrated Security=True;"
Microsoft.EntityFramework.sqlserver -outputdir Models
I get this error:
scaffold-dbcontext "Data Source=(local);Initial Catalog=MyDB;In ...
CategoryInfo : ObjectNotFound: (scaffold-dbcontext:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
I know how to resolve this if I was using Entity Framework Core but I am using Entity Framework 6.0.0 and all the solutions I have found address the Entity Framework Core version.
Thank you.
I tried to add EntityFramework.Tools in the Nuget Package Manager but I could not find it. I also tried to include it in the .csproj file but it didn't work. It appears that there is no EntityFramework.Tools dll.

For EF 6 use the built in VS tooling

Related

No DbContext found in Console App using EF Core dotnet EF migration add results in No DbContext found

Please note this is a CONSOLE app.
The Problem
When using dotnet ef migrations add InitialCreate --context Efc.Models.ApplicationDbContext I receive
the following message: "No DbContext named 'Efc.Models.ApplicationDbContext' found"
I am running the dotnet command in the EFConsole.ConUI directory. The spellings seem to be correct.
Using VS2019, EntityFramework Core 3.1.3
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(#"Server=SSSSSSSS\XXSQLSERVER;Database=PhoneNumberTest;User ID=XXXXXXXX;Password=XXXXXXXX");
}
The following packages are installed as indicated:
EFConsole.ConUI Console project
Microsoft.EntityFrameworkCore.Design
Microsoft.EntityFrameworkCore.Tools
Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore
Efc.Models -- class library for models and ApplicationDbContext
Microsoft.EntityFrameworkCore.SqlServer
Efc.RepositoryLayer -- class library for the repositories.
Microsoft.EntityFrameworkCore.SqlServer
If I move the ApplicationDbContext to the EFConsole.ConUI project the dotnet ef command works. But for obvious reasons the ApplicationDbContext needs to be in a separate project so that it can be referenced from the Efc.Repository project at a minimum.
EDIT:
Note: Because this is a console APP I do not have a Startup class and I have not loaded any services like one would in a WebApp in its Startup class. Could this be the problem??
I have searched and read until my eyes were bleeding. :(
Thanks for any guidance.
Try opening the Nuget Package Manager Console:
In Visual Studio: Tools -> NuGet Package Manager => Package Manager Console
Select the default project to the one where the DbContext class is:
Run the command:
add-migration InitialCreate

EF Core Migrations in Separate Project - Issues

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

Entity Framework "update-database" fails in on dev env with error "Microsoft.VisualStudio.Data.Tools.Package is not marked as serializable"

I recently moved from my old dev environment running VS 2015, .Net 4.5.1 on
Windows 8.1 to a new environment running VS 2017, .Net 4.6.2 on Windows 10.
Previously I was able to call update-database -script from the package manager console with no problem. Though since the move I am struck with this strange error.
Has anyone else run into it, and if do you have a fix?
Exception calling "SetData" with "2" argument(s): "Type 'Microsoft.VisualStudio.Data.Tools.Package.Internal.OAProject' in assembly 'Microsoft.VisualStudio.Data.Tools.Package, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' is not marked as serializable."
At C:\Workspace\Projects\XXX\main\packages\EntityFramework.6.1.3\tools\EntityFramework.psm1:720 char:5
+ $domain.SetData('startUpProject', $startUpProject)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : SerializationException
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.GetProjectTypes(Project project, Int32 shellVersion)
at System.Data.Entity.Migrations.Extensions.ProjectExtensions.IsWebProject(Project project)
at System.Data.Entity.Migrations.MigrationsDomainCommand.GetFacade(String configurationTypeName, Boolean useContextWorkingDirectory)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Object reference not set to an instance of an object.
I see that #lincolnzocateli makes mention of this problem here https://github.com/aspnet/EntityFramework/issues/7031 and then here https://github.com/aspnet/EntityFramework6/issues/156 but the solution is rather vague.
Hate answering my own question...
The simple fix seems to be that you need to specify the startup project in packaged manager on VS 2017.
So rather than running this code
update-database
You need to run the following code
update-database -StartupProject "XXXXX"

add-migration : Cannot bind argument to parameter 'Path' because it is an empty string

I'm running default api core project on Visual studio 2017 with docker support and
I got this error everytime I run the command add-migration -name name
add-migration : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:1
+ add-migration
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Migration], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Add-Migration
PS : This command works without docker support, but return this error when I add docker support
It looks like you're hitting issue #7540. For now, put your project on a "physical" drive instead of a network file share. (Scratch that. You're using the PMC commands.)
You're probably hitting issue #7439. Make sure your app is selected as the startup project (or use the -StartupProject parameter) when running the commands.
I tried many suggested solutions and none seemed to work except for this: https://stackoverflow.com/a/58592444/1819403
It would be interesting to know with which version of Entity Framework this error occurred.
I had the same error with EF 6.3.0 and I am not using docker at all.
The solution for me was to go back to version 6.2.0 and create the migration with it.

.Net Core can't use Add-migrations with Docker support

I'm stuck for using EF command with docker support on an ASP.NET Core project.
I created an ASP.NET Core API basic project using Visual Studio 2017 RC.
I added dependencies and configurations needed by Entity Framework to work.
I ran Add-migration "name" and Update-database and everything works fine as expected.
But since I added Docker support on the project, I receive the error below each time I run an EF command:
Add-migration : Cannot bind argument to parameter 'Path' because it is an empty string.
At line:1 char:1
+ Add-migration init-database
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Add-Migration], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Add-Migration
As I saw on many tutorials, there is no need to update Docker file or Docker compose file in order to make EF commands available but maybe I'm wrong.
PS: Docker works pretty fine, I'm able to run the project through Docker without any problems. Since Docker support creates another project in the solution, I already tried to go on the app project path to run the command instead of solution path, but it doesn't change anything.
Enable docker support add the project "docker-compose" to the solution.
So just set your initial app project as the startup project fix the problem
Or run any EF Command with -startupproject "yourinitialappproject"

Resources