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

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

Related

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.

To handle multiple schemas in one DBContext

I am using an existing Database with a new ASP.Net Core 2.0 application. The database has two schemas, dbo and notinapplication. I do not want to create model of notinapplication schema tables. So I use the following code in Package manager and it works fine.
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "dbo"
This way I only get tables from dbo in DbContext and the notinapplication schema tables are ignored.
However now I have a new schema called user that needs to be part of the model.
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "user"
But using the above code eliminates the tables from dbo schema. What are my options to have tables of both schemas in DBContext while ignoring the notinapplication schema.
And if I indeed have to create different contexts, is it possible to query from multiple DB contexts in one query?
All you need to do to provide multiple values is to use the 'array syntax'.
-Schema "schema1","schema2","schema3"
In your case, you have to do
Scaffold-DbContext "Server=localhost; Database=TestServer; Trusted_Connection=True;
MultipleActiveResultSets=true;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models
-UseDatabaseNames -Force -Context "DbContext" -Schema "dbo","user"
You can do the job by using -Schema parameter multiple times in this way though :/
-Schema "dbo" -Schema "user"

ApplicationDBContext AspUsers Migration command

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

Scaffolding existing databases in ASP.NET Core (Scaffold-DbContext)

Right now, the only way (as far as I know) to scaffold a database is via the command: Scaffold-DbContext .....
This does not scaffold tables without primary keys or views and does not run if there are any errors in your code. Also, I believe in order to update one table, you have to scaffold the entire database again (correct me if I'm wrong)
Will we see something like .edmx files in past ASP.NET versions? Something with a GUI or just less error-prone?
Is there another way to do it that I've missed?
We can Scaffold the entire database tables with following Package Manager Console Command
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir 'directory to save the scaffolded tables'
To Scaffold one table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name
To Update the existing table
Scaffold-DbContext "Server=yourserver;Database=database;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir testDir -t table-name -force -verbose

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