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.
Related
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
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"
I'm working on a project with different implementations of DbContext. Using EF code first, I generate migration :
Enable-Migrations -ContextTypeName <> -Force
Add-Migration <>_DB_v1.0
Update-Database -Verbose
The update always tries to attach the *.mdf file but no file is ever genrated in local, and I'm working on a distant SQL Server (Distant virtual DB)
the Update-Script option successfully generates the SQL file and well executed on the Db, but I always get error to execute last update before going on new migration.
Could any body help ?
Project is in .net 4.6, and using EF 6.0.
Sorry not able to paste code for confidentiality. I may ensure that All connection string, project conf. and so are perfectly set.
Please ensure that migration history table in the Db and Migrations files in your Solution are match.
I think you miss some migration.
I found the error reason:
1- Indicate explicitly the providerName="System.Data.SqlClient" in the connectionStrings definition
2- Executing the migration, specify the -ConnectionStringName, even if the name displaed in the console seems to be OK.
Add-Migration DB_vX.x -ConnectionStringName XXX_Database
update-database -Verbose -Script -ConnectionStringName XXX_Database
I'm attempting to setup store for my IdentityServer4 Token server,
I'm following along with this tutorial where I encountered database migrations like so:
dotnet ef migrations add InitialIdentityServerMigration -c PersistedGrantDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ConfigurationDbContext
dotnet ef migrations add InitialIdentityServerMigration -c ApplicationDbContext
Apparently I'm running a different version of powershell, or the tools are messed up so I have to run my migrations using a different syntax
Add-Migration InitialIdentityServerPersistedGrantDbMigration -c PersistedGrantDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
Add-Migration InitialIdentityServerConfigurationDbMigration -c ConfigurationDbContext -o Data/Migrations/IdentityServer/ConfigurationDb
Theese two commands worked great however I'm still missing the third command to generate the ApplicationDbContext:
When I run I get this exception:
SqlException: Invalid object name 'AspNetUsers'.
I'm missing this table along with a few others from the database for Identity, does anyone know which Migration to use?
If you create the default ASP.NET project with authentication, you will see a migration file 00000000000000_CreateIdentitySchema.cs.
So
dotnet ef migrations add CreateIdentitySchema -c ApplicationDbContext
should work.
It turns out it was simple. I though Migrations were generating the Db from something that it had in code, but in actuality it seems to be constructing the database based off of the CLR Class configuration
Add-Migration InitialStillDontKnowWhatThisNameIsFor -c ApplicationDbContext -o Data/Migrations/IdentityServer/PersistedGrantDb
I don't know what the names is for but that will generate the DB
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