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?
In the Oracle database system, the term database schema, which is also known as "SQL schema," has a different meaning. Here, a database can have multiple schemas (or “schemata,” if you're feeling fancy). Each one contains all the objects created by a specific database user.
Scaffold-DbContext commands help scaffolding entity type classes and a DbContext class based on a database schema thereby automating the code generation technique related to database access.
As with any code-first schema customization, you can do this by using the entity classes' attributes or through the DbModelBuilder API. With data annotations, you can use the optional second parameter of the Table attribute to specify the schema name. The code in Figure 3 implements this change in the model.
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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With