实体框架:数据库上下文可以在不同的模式之间具有外键吗? [英] Entity Framework: Can DB Contexts Have Foreign Keys Across Different Schemas?

查看:74
本文介绍了实体框架:数据库上下文可以在不同的模式之间具有外键吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们只有一个具有多个架构的数据库。公司架构师希望每个架构都有Entity Framework DbContext。此外,不同的模式之间存在外键关系。

We have a single database with multiple schemas. Company architect wants to have Entity Framework DbContext for each schema. Additionally, foreign key relationships exist between different schemas.

在今天的脚手架中,收到此错误:

In scaffolding a schema today, received this error:

错误:


对于表CustomerTransaction上的外键FK_Customer_TransactionId,无法在主体上建模外键的结尾表inv.Product。这通常是因为主体表未包含在选择集中。

For foreign key FK_Customer_TransactionId on table CustomerTransaction, Unable to model the end of the foreign key on principal table inv.Product. This is usually because the principal table was not included in the selection set."

我需要一个模式和另一个表

I need to have one schema and 1 table from another schema. It cannot locate the Parent Table on the additional schema to create foreign key off.

寻找解决方案:

(1)通常,每个模式都具有多个数据库上下文并且在不同模式之间使用外键是否是不好的做法?Microsoft和Entity Framework是否支持此操作?

(1) Is it bad practice in general to have multiple db contexts for every schema, with foreign keys between different schemas? Do Microsoft and Entity Framework support this?

(2)在Entity Framework Core脚手架中,是否仍然要脚手架一个模式以及另一个模式中的另一个父表?

(2) In Entity Framework Core Scaffold, Is there anyway to scaffold a schema And an additional parent table from another schema?

(3)还是有忽略外部的方法?

(3) Or is there method to ignore foreign key property on outside schemas?

dotnet ef dbcontext scaffold 
   "Server=localhost;Database=DatabasetestTest;Trusted_Connection=True;" 
   Microsoft.EntityFrameworkCore.SqlServer -c DatabaseContext 
   -o Entities\Scaffold 
   -f 
   --project C:\Project 
   --schema CustomerService

当前使用EF Core 2.2

Currently using EF Core 2.2

实体框架:一个数据库,多个DbContext。

使用具有多个DB模式但使用一个DBContext的Entity Framework 6

推荐答案


(1)通常,在每个模式中为
拥有多个数据库上下文,并且在不同模式之间使用外键是否是一种不好的做法?
Microsoft和Entity Framework是否支持此功能?

(1) Is it bad practice in general to have multiple db contexts for every schema, with foreign keys between different schemas? Do Microsoft and Entity Framework support this?

我认为使用多个架构或/ and DbContext 或方案之间的关系。实体框架支持它。无论如何,我建议考虑这样做,因为将来您最终可能会拥有太多的数据库上下文,并且可能会变得一团糟。

I don't think there is a bad practice in having multiple schemas or/and DbContext or relationships between schemes. Entity Framework supports it. Anyway, I'd suggest to think about doing it as in the future you can end up having too many database contexts and it may become a big mess.


(2)在Entity Framework Core脚手架中,是否仍然需要脚手架
模式和另一个模式的另一个父表?

(2) In Entity Framework Core Scaffold, Is there anyway to scaffold a schema And an additional parent table from another schema?

是。您可以设置多个方案和表。唯一的事情是您必须指定要使用的具体方案和表。

It is. You can scaffold multiple schemes and tables. The only thing is you have to specify concrete schemes and tables to scaffold.

dotnet ef dbcontext scaffold 
   "Server=localhost;Database=DatabasetestTest;Trusted_Connection=True;" 
   Microsoft.EntityFrameworkCore.SqlServer -c DatabaseContext 
   -o Entities\Scaffold 
   -f 
   --project C:\Project 
   --schema CustomerService
   --schema AnotherSchema
   --schema YetAnotherSchema
   --table Table1
   --table Table2
   --table Table3
   --table TableX




(3)还是有方法忽略
外的外键属性模式?

(3) Or is there method to ignore foreign key property on outside schemas?

我不这么认为。每当两个表相互之间具有关系时,您都必须使其一致。如果要支撑FK在表 inv.Product 中引用的表 CustomerService.CustomerTransaction ,则必须包含架构 inv并将表 Product添加到脚手架。您可能会遇到另一个错误,因为与要包含的表可能存在其他关系。您可能最终要搭建整个数据库。

I don't think so. Whenever two tables have relation between each other you have to conform it. If you want to scaffold table CustomerService.CustomerTransaction that is referenced by FK in table inv.Product you have to include schema 'inv' and table 'Product' to the scaffolding. You might get another error as there might be other relations with tables you going to include. You may end up scaffolding whole database.

这篇关于实体框架:数据库上下文可以在不同的模式之间具有外键吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆