简体   繁体   English

在一个DBContext中处理多个模式

[英]To handle multiple schemas in one DBContext

I am using an existing Database with a new ASP.Net Core 2.0 application. 我正在使用一个新的ASP.Net Core 2.0应用程序的现有数据库。 The database has two schemas, dbo and notinapplication. 该数据库有两个模式,dbo和notinapplication。 I do not want to create model of notinapplication schema tables. 我不想创建notinapplication架构表的模型。 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. 这样我只能在DbContext中从dbo获取表,并忽略notinapplication模式表。

However now I have a new schema called user that needs to be part of the model. 但是现在我有了一个名为user的新模式,需要成为模型的一部分。

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. 但是使用上面的代码消除了dbo模式中的表。 What are my options to have tables of both schemas in DBContext while ignoring the notinapplication schema. 我可以选择在DBContext中同时拥有两个模式的表,同时忽略notinapplication模式。

And if I indeed have to create different contexts, is it possible to query from multiple DB contexts in one query? 如果我确实必须创建不同的上下文,是否可以在一个查询中从多个DB上下文进行查询?

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"

您可以通过以下方式多次使用-Schema参数来完成工作 :/

 -Schema "dbo" -Schema "user"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM