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"
Related
I tried to use the reverse engineering method as mentioned here. I had to do that because of the changes in a few table structures. After this, the models are not recognized as available resources, although the folder structure is the same. This is the folder structure:
-Data
--- DBContexts
------ ContextFile.cs
--- Models
------ Entity.cs
I ran the following command for the process:
Scaffold-DbContext '<connection-string>' Npgsql.EntityFrameworkCore.PostgreSQL -contextdir Data/DbContexts -outputdir Data/Models -context ContextFile -Force
I had to use the -Force flag to overwrite the previous generated models.
I am getting the following error when I try to use the models:
type or namespace name could not be found.
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).
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
Let's say I have migrations
Initial.cs
foo.cs
bar.cs
baz.cs
and my database is current "in sync" with applying
Initial.cs
foo.cs
and what I want to do is the equivalent of
Try applying bar.cs and then baz.cs, all in a transaction. If it fails, roll back.
Is that possible using Update-Database or migrate.exe with parameters or does it request special Powershell script or even custom C# logic?
I don't think this is possible, but you can easily revert to a previous migration as EF allows that:
Update-Database -TargetMigration "PreviousMigrationIdentifer"
-ConnectionProviderName "System.Data.SqlClient" -Force
You can even use a connection switch to do that on Dbs having issues with migrations applied:
-ConnectionString "Data Source=(LocalDB)\Tests;Initial Catalog=DnbName;..."
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