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

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

问题描述

它是一个很好的做法,有多个 XXX:的DbContext 类为Web应用程序的每个主要部分(考虑到它是一个大的,在它的数据库中至少有50桌)?例如:MembershipContext,BlogContext,StoreContext等还是会更方便有一个 DatabaseContext 所有DB访问相关的东西。

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.

不管怎样,你应该看看库和模式的UnitOfWork有如何使用您的DbContexts abstractation层。看看这里:多DbContexts在N层应用程序这里<一个href=\"http://stackoverflow.com/questions/9140167/ef-and-repository-pattern-ending-up-with-multiple-dbcontexts-in-one-controller\">EF和存储库模式 - 在一个控制器中结束了与多个DbContexts - ?的任何问题(性能,数据完整性)如果你使用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桌,我认为这可能是合理的有多个DbContexts。因此,我建议使用多个DbContexts。但是,你应该使用库和模式的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).

我希望帮助。

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

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