单个 Web 应用程序的多个 DbContext 类.是好是坏? [英] Multiple DbContext classes for a single web app. Good or bad?

查看:20
本文介绍了单个 Web 应用程序的多个 DbContext 类.是好是坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为 Web 应用程序的每个主要部分设置多个 XXX : DbContext 类是否是一个好习惯(考虑到它的数据库中至少有 50 个表)?例如:MembershipContext、BlogContext、StoreContext 等.或者为所有与数据库访问相关的内容使用一个 DatabaseContext 更方便.

Is it a good practice to have multiple XXX : DbContext classes for each major section of a web application (considering it's a big one with at least 50 tables in its database)? For example: MembershipContext, BlogContext, StoreContext etc. Or it's more convenient to have a single DatabaseContext for all the db access related stuff.

推荐答案

使用多个 DbContext 类意味着复杂的交叉事务(你可以在网上找到解决这个问题的例子 http://pastebin.com/YEDqyH0n) 但可能是合理的.这完全取决于您的架构和您想要设计的分离.

Using multiple DbContext classes implies complicating cross-transactions (you can find a solution to this problem on the web here an example http://pastebin.com/YEDqyH0n) but may be justified. It all depends on your architecture and the separation that you want to design.

无论如何,您应该查看 Repository 和 UnitOfWork 模式,以对如何使用 DbContext 进行抽象.看这里:N 层应用程序中的多个 DbContexts 和这里 EF 和存储库模式 - 以多个 DbContext 合而为一控制器 - 任何问题(性能、数据完整性)? 如果您使用 ASP.NET MVC.

Anyways you should look at the Repository and UnitOfWork patterns to have a layer of abstractation of how to use your DbContexts. Look here: Multiple DbContexts in N-Tier Application and here EF and repository pattern - ending up with multiple DbContexts in one controller - any issues (performance, data integrity)? if you use ASP.NET MVC.

对于 50 个表,我认为拥有多个 DbContext 可能是合理的.所以我建议使用多个 DbContexts.但是您应该使用 Repository 和 UnitOfWork 模式将它们包装起来,以使其独立于其他层中的实际实现(这样您以后很容易改变主意,例如只使用一个信号 DbContext).

For 50 tables I think it may be justified to have multiple DbContexts. So I would recommend using multiple DbContexts. But you should wrap them using the Repository and UnitOfWork patterns to be independant from the actual implementation in the other layers (like this you could easily change your mind later and only use a signle DbContext for example).

希望能帮到你.

这篇关于单个 Web 应用程序的多个 DbContext 类.是好是坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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