No DbContext found in Console App using EF Core dotnet EF migration add results in No DbContext found - asp.net-mvc

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

Related

Database migration for .Netcore MVC project in Mac

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

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

.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"

Entity Framework migrate.exe not working after updating to AspNet.Identity

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.

Run Migrations command if not have visual studio

I'm publishing a website ASP.NET MVC and get this error:
“Migrations is enabled for context ‘Context’ but the database does not exist or contains no mapped tables. Use Migrations to create the database and its tables, for example by running the ‘Update-Database’ command from the Package Manager Console.”
I can't install visual studio on server so i can't run ‘Update-Database’ command.
How to solve this problem ?
Find migrate.exe (YourApp\packages\EntityFramework.6.1.3\tools) and paste it in bin folder on the server.
Open Command Prompt on the server.
Run below commands
cd "C:\Websites\YourApp\bin" migrate.exe YourAppName.Api.dll /startupconfigurationfile:..\Web.config /verbose
Please Note that YourAppName.Api.dll should be the DLL having Enabled Migration.
you do not need to install visual studio on the server.
if you have your database connection string setup in web.config you should be able to run update-database in the package manager console and it will update your database on your web host.

Resources