Enable Migrations does not work in Package Manager Console - c#

I installed EF in my soluton:
PM> Install-Package EntityFramework -IncludePrerelease
After that, I am trying to execute enable migration command, but it does not work:
PM> enable-migrations
Error:
No context type was found in the assembly 'MyProjectName'.

Specify the target project (that has the DbContext derived class) from the drop down menu Default Project, or alternatively:
PM> enable-migrations -Project <MyProjectName>

try this command may help.
EntityFrameworkCore\enable-migrations

Related

Add-Migration : A parameter cannot be found that matches parameter name 'Context'

Add-Migration : A parameter cannot be found that matches parameter name 'Context'.
At line:1 char:15
+ Add-Migration -Context ManagementSystemContext
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Add-Migration], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Add-Migration
I'm trying to add migration to App but I got this error despite using this command:
PM> Add-Migration -Context ManagementSystemContext
I uninstalled EntityFrameWork.tools then reinstalled it and restarted visual studio and still the same error
I had the same problem and I couldn't realize what it is. After 2-3 restarts of the PC and the VS, making the same command
Add-Migration NameOfTheMigration -Context MyContext
it appear something in the lines of
"if you are using EntityFramework or EntityFrameworkCore, make sure that you type EntityFramework\Add-Migration, or EntityFrameworkCore\Add-Migration".
I did that and the migration was on.
So the command line looked like this:
EntityFrameworkCore\Add-Migration NameOfTheMigration -Context MyContext
I'm guessing that there were both, EF and EFCore, so the PMC was confused.
Anyway, this is what worked for me.
After "Add-Migration" command EF first expects the name of the migration.
You can cheek this documentation and code sample to see if you did something wrong.
Also you can try to do it via .net core CLI instead of Package-Manager Console, like:
dotnet ef migrations add MigrationName --context ManagementSystemContext --output-dir Dir
Hope something will help.
If your project has two contexts as well as Multi Startup. You must do the following steps:
1- First, take the project out of Multi StartUp mode and set the project startup to your desired Startup.
Then in the Package Manger Console section, type the following command:
Add-Migration -Context ContextName
NOTE : Your default Project in package manger console must set as your DataAccess class libarary

error: NU1100: Unable to resolve 'MicrosoftOfficeCore (>= 15.0.0)' for 'net5.0'

In Terminal of Visual Studio Code, when I try to run:
dotnet add package MicrosoftOfficeCore --version 15.0.0
I get the following error on Visual Studio Code terminal:
error: NU1100: Unable to resolve 'MicrosoftOfficeCore (>= 15.0.0)' for 'net5.0'
error: Package 'MicrosoftOfficeCore' is incompatible with 'all' frameworks in project
Do anyone have an idea?
Try clearing the NuGet cache using dotnet nuget locals all --clear, and then try to add your package.
If that doesn't work, try deleting the NuGet.config file present inside C:\Users\<username>\AppData\Roaming\NuGet directory, and then restore it using dotnet restore command. Try adding your package after this.

Enable-Migrations is obsolete. Use Add-Migration to start using Migrations

Ok, but what about the following settings that we were using before the Configuration is obsolate?
public Configuration()
{
//AutomaticMigrationsEnabled = true; //it is Ok for now as default value is true
//what about the following settings???
AutomaticMigrationDataLossAllowed = true; //Attention when using this!!!
MigrationsDirectory = #"Migrations";
ContextKey = "Demo.Domain.DemoDbContext";
}
Where can I set these settings?
Install EntityFrameworkCore.Tools package for using commands like Add-Migration and Update-Database in Package Manager Console. You don't need to call Enable-Migrations, and as a side note AutomaticMigrationsEnabled is obsolete.
In the PM Console you can always run the --help command with:
dotnet ef migrations add --help.
Then you will be able to see all the configuration needed:
Usage: dotnet ef migrations add [arguments] [options]
Arguments:
<NAME> The name of the migration.
Options:
-o|--output-dir <PATH> The directory (and sub-namespace) to use. Paths are relative to the project directory. Defaults to "Migrations".
--json Show JSON output.
-c|--context <DBCONTEXT> The DbContext to use.
-p|--project <PROJECT> The project to use.
-s|--startup-project <PROJECT> The startup project to use.
--framework <FRAMEWORK> The target framework.
--configuration <CONFIGURATION> The configuration to use.
--runtime <RUNTIME_IDENTIFIER> The runtime to use.
--msbuildprojectextensionspath <PATH> The MSBuild project extensions path. Defaults to "obj".
--no-build Don't build the project. Only use this when the build is up-to-date.
-h|--help Show help information
-v|--verbose Show verbose output.
--no-color Don't colorize output.
--prefix-output Prefix output with level.
For example if you want to set the directory to Migrations (it is the default):
dotnet ef migrations add MyMigration --output-dir Migrations
Or add a migration while specifying the DbContext:
dotnet ef migrations add MyMigration --context MyDbContext
Enable-Migrations was for the older version. new version support to the Add-migration
first enter add-migration "setupName" .
setup name is a any recognizable name eg: add migration "initialSetup"

Unrecognized option '-Context' when running dotnet ef command

Starting a new project, and trying to set up the database for authentication. I run the following command in powershell.
dotnet ef database update -Context ApplicationDbContext
and get the following error Unrecognized option '-Context'
So I try running it without the -Context option just to see and get:
More than one DbContext was found. Specify which one to use. Use the '-Context' parameter
for PowerShell commands and the '--context' parameter for dotnet commands.
I also tried running it with --context instead of -context, but get the same error. Any suggestions on why it recognizes that I need that option, but at the same time tells me it doesn't recognize the option?
I also restart powershell.
Instead of using --context ApplicationDbContext have you tried -c ApplicationDbContext
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#aspnet-core-environment
I was having the same problem and instead of using dotnet ef database update -Context ApplicationDbContext, I replaced -Context with -c which worked for me.

Visual Studio Code Entity Framework Core Add-Migration not recognized

I've used yoman to generate an ASP.Net Core Web API application via the Visual Studio Code Editor. For reference, I followed this tutorial here.
The API works fine. However, I am trying to use EntityFramework Core Migrations with SQL Server. When I type the following into the Visual Studio Code Terminal:
Add-Migration MyDbInitialMigration
I get the following message:
'Add-Migration' is not recognized as an internal or external command, operable program or batch file.
I have the Microsoft.EntityFrameworkCore.Tools: 1.1.0-preview4-final dependency installed. I did this using the .Net Core Project Manager (Nuget) extension.
In Visual Studio 2015 this command works fine from the Package Manager Console.
I assume that using Visual Studio Code's Terminal is the problem. But does anyone know how I can use EF Core Migrations from within the VSCode editor itself?
Solution
Running the dotnet ef migrations add InitialCreate command yielded the following error:
No executable found matching command "dotnet-ef"
To solve this I needed to install the following dependency, And add it to the tools section:
Microsoft.EntityFrameworkCore.Tools.DotNet
The correct format to add a new migration is:
dotnet ef migrations add yourMigrationName
and to update database is:
dotnet ef database update
You need to add:
dotnet tool install --global dotnet-ef
Im working on Mac, so Ruby is installed by default. My EF commands required lots of extra parameters --project, --startup-project etc. This was a pain to type every time, so I used rake to make this easier.
In my project root, I added a file called rakefile with these contents:
desc "Add Migraion"
task :'add-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations add " + ARGV[1] + " --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj "
end
desc "Remove Migraion"
task :'remove-migration' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef migrations remove --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
desc "Update Database"
task :'update-database' do
ARGV.each { |a| task a.to_sym do ; end }
puts ARGV[1]
sh "dotnet ef database update --project MyProject.Data/MyProject.Data.csproj --startup-project MyProject.Web/MyProject.Web.csproj"
end
Then at the command line, I run these commands:
rake add-migration <migrationName>
rake remove-migration
rake update-database
First we need to add reference in *.csproj file in the following way
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.2" />
</ItemGroup>
in Bash/Command prompt
dotnet restore
after that
dotnet ef migrations add MyDbInitialMigration

Categories

Resources