ApplicationDBContext AspUsers Migration command - c#

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

Related

Pomelo C# : i can't import Table without Data

I can't import with Pomelo on C#, a several tables without data.
Scaffold-DbContext "'Database'" Pomelo.EntityFrameworkCore.MySql" -Table 'table1' 'table2
I have an error :
sequence contains no matching element
Do you have any solution?
I have resolve the problem but i'm not sur it's good practice.
I import the all table with :
Scaffold-DbContext "'Database'" Pomelo.EntityFrameworkCore.MySql"
and I delete the files of others tables that I don't use.
Scaffolding a database and stating individual tables works fine with Pomelo 3.2.2:
CLI:
dotnet ef dbcontext scaffold "server=127.0.0.1;uid=root;pwd=;port=3306;database=So64222039" Pomelo.EntityFrameworkCore.MySql -c MyContext --verbose -t IceCreams -t IceCreamShops
Package Manager:
Scaffold-DbContext "server=127.0.0.1;uid=root;pwd=;port=3306;database=So64222039" Pomelo.EntityFrameworkCore.MySql -Context MyContext -Verbose -Tables IceCreams,IceCreamShops
The syntax to specify multiple tables is different when using the VS Package Manager instead of the CLI (use -Tables table1,table2,table3 instead of -t table1 -t table2 -t table3).

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

How to set a startup project to update a database in a published ASP.NET5 | MVC6 project?

There are two EF7 db-contexts in a MVC6 (beta-7) project. First of them (let's call it StartupContext) is in a startup project (MyProject.Startup). Second (let's call it DataContext) is in a separate assembly (MyProject.Data).
There is a command in the project.json files of the MyProject.Startup and the MyProject.Data:
"dependencies": {
"EntityFramework.SqlServer": "7.0.0-beta7",
"EntityFramework.Commands": "7.0.0-beta7",
...
}
"commands": {
"ef": "EntityFramework.Commands"
...
}
I can use it for a database updating like this:
for an updating of the StartupContext I run the ef command from a startup folder:
dnx ef database update
for an updating of the DataContext I run the ef command from a folder of the MyProject.Data project:
dnx ef database update -c DataContext -s MyProject.Startup
It works perfectly but only from sources (from not published project). I get exception like this for an updating of the DataContext from a published project:
System.IO.FileNotFoundException: Could not load file or assembly "MyProject.Data" or one of its dependencies.
I tried various approaches:
dnx ef database update -c DataContext -s MyProject.Startup
dnx ef database update -c DataContext -s C:\Published\approot\packages\MyProject.Startup
dnx ef database update -c DataContext -s C:\Published\approot\packages\MyProject.Startup\1.0.0\root\
dnx ef database update -c DataContext -s C:\Published\approot\packages\MyProject.Startup\1.0.0\lib\MyProject.Startup.dll
dnx ef database update -c DataContext -s %~dp0approot\packages\MyProject.Startup
dnx ef database update -c DataContext -s %~dp0approot\packages\MyProject.Startup\1.0.0\root\
I also tried to copy the MyProject.Startup.dll to a folder with the MyProject.Data but unsuccessfully.
Has anybody else had a similar problem?
How can I update a database and create a migration from a published project?
This is a bug in EF7-beta7 and should be fixed in the upcoming RC1 release of EF.
If you need (as in very much so) this feature before the, you could use the nightly builds of EF. (Caution: nightly builds have a lot of instability.)

Categories

Resources