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

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

问题描述

我们有一个包含多个模式的数据库.公司架构师希望每个架构都有实体框架 DbContext.此外,不同模式之间存在外键关系.

今天在搭建架构时,收到此错误:

错误:

<块引用>

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

我需要一个架构和另一个架构中的 1 个表.它无法在附加架构上找到父表来创建外键.

寻找解决方案:

(1) 通常情况下,每个模式都有多个 db 上下文,在不同模式之间使用外键是不好的做法吗?Microsoft 和 Entity Framework 是否支持此功能?

(2) 在 Entity Framework Core Scaffold 中,有没有办法从另一个 schema 中构建一个 schema 和一个额外的父表?

(3) 或者有没有方法可以忽略外部模式的外键属性?

dotnet ef dbcontext 脚手架服务器=本地主机;数据库=DatabasetestTest;Trusted_Connection=True;"Microsoft.EntityFrameworkCore.SqlServer -c DatabaseContext-o 实体脚手架-F--project C:Project--模式客户服务

目前使用 EF Core 2.2

实体框架:一个数据库,多个数据库上下文.这是个坏主意吗?

使用实体框架 6多个数据库架构但使用一个 DBContext

解决方案

(1) 通常情况下,为多个数据库上下文设置多个数据库上下文是不好的做法吗?每个模式,在不同模式之间都有外键?做微软和实体框架支持这个吗?

我不认为拥有多个模式或/和 DbContext 或模式之间的关系是一种不好的做法.实体框架支持它.无论如何,我建议考虑这样做,因为将来您最终可能会拥有过多的数据库上下文,并且可能会变得一团糟.

<块引用>

(2) 在 Entity Framework Core Scaffold 中,有没有办法搭建一个架构和来自另一个架构的额外父表?

是的.您可以搭建多个方案和表格.唯一的问题是您必须指定要搭建的具体方案和表格.

dotnet ef dbcontext 脚手架服务器=本地主机;数据库=DatabasetestTest;Trusted_Connection=True;"Microsoft.EntityFrameworkCore.SqlServer -c DatabaseContext-o 实体脚手架-F--project C:Project--模式客户服务--schema AnotherSchema--schema YetAnotherSchema--table 表 1--table 表 2--table 表 3--table TableX

<块引用>

(3) 或者有什么方法可以忽略外部的外键属性模式?

我不这么认为.每当两个表相互之间有关系时,您必须符合它.如果您想在表 inv.Product 中构建由 FK 引用的表 CustomerService.CustomerTransaction,您必须将架构inv"和表Product"包含在脚手架中.您可能会收到另一个错误,因为可能与您要包含的表存在其他关系.您最终可能会搭建整个数据库.

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:

Error:

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.

Looking for solutions:

(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) In Entity Framework Core Scaffold, Is there anyway to scaffold a schema And an additional parent table from another schema?

(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 EntitiesScaffold 
   -f 
   --project C:Project 
   --schema CustomerService

Currently using EF Core 2.2

Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

Using Entity Framework 6 with Multiple DB Schemas but using One DBContext

解决方案

(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?

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) 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 EntitiesScaffold 
   -f 
   --project C:Project 
   --schema CustomerService
   --schema AnotherSchema
   --schema YetAnotherSchema
   --table Table1
   --table Table2
   --table Table3
   --table TableX

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

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天全站免登陆