Reputation: 1686
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?
Upvotes: 10
Views: 14112
Reputation: 1925
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"
Upvotes: 23
Reputation: 655
You can do the job by using -Schema
parameter multiple times in this way though :/
-Schema "dbo" -Schema "user"
Upvotes: 2