EF Core Migrations in Separate Project - Issues - entity-framework-migrations

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

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

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

How to use ef6.exe

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

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.

codefirst - model changes commands

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.

Resources