Using Entity I currently have dbcontext that has every table in it.
使用实体我目前有dbcontext,其中包含每个表。
I'm wondering if that's what everyone does, or do you have a context per module for example. To me the dbcontext was a connection to map the models to a database, and since there is only one database, I only need one.
我想知道这是否是每个人都做的,或者你是否有每个模块的上下文。对我来说,dbcontext是一个将模型映射到数据库的连接,由于只有一个数据库,我只需要一个。
Before I get too far along I want to see if that's appropriate.
在我走得太远之前,我想看看这是否合适。
So 1 db context per database or many?
那么每个数据库1个db上下文还是很多?
2 个解决方案
#1
4
I went through this same process recently and found some great resources on the subject. Here are a couple that were very helpful:
我最近经历了同样的过程,并在这个主题上找到了一些很好的资源。这是一对非常有帮助的夫妇:
- Shrink EF Models with DDD Bound Contexts.
- 使用DDD绑定上下文缩小EF模型。
- How to decide on a lifetime for your ObjectContext.
- 如何确定ObjectContext的生命周期。
I was building a Desktop app, and I ended up using multiple contexts so that I could keep the lifetime tied to the module rather than the application. This has worked out very well for me, and I like that my DbContext
isn't swamped with DbSets
and is limited to the ones that are relevant for the current module.
我正在构建一个桌面应用程序,最后我使用了多个上下文,这样我就可以将生命周期与模块而不是应用程序联系起来。这对我来说非常好,我喜欢我的DbContext没有被DbSets淹没,并且仅限于与当前模块相关的那些。
In an ASP.NET MVC app, it is different since the DbContext
will only live as long as the request, and in those cases, I usually use a single DbContext
to simplify things unless the database is very large. With a large database, I would probably break it apart into multiple DbContexts
just to limit the overhead and the clutter, and keep things compartmentalized.
在ASP.NET MVC应用程序中,它是不同的,因为DbContext只会在请求时生存,在这种情况下,我通常使用单个DbContext来简化操作,除非数据库非常大。有了一个大型数据库,我可能会把它拆分成多个DbContexts,只是为了限制开销和杂乱,并保持分区。
#2
0
currently the EF hasnt been made to be broken down into diff dbContexts. Here a great talk about it
目前EF尚未被分解为diff dbContexts。这里有一个很好的谈论它
What we have done for this case that we have made a project diff from our MVC website just for the database generation and then have separate dbContexts for every requirement.
我们为这个案例做了什么,我们已经从我们的MVC网站创建了一个项目差异,只是为了生成数据库,然后为每个需求都有单独的dbContexts。
This way our dbContexts are never large and are easy to maintain
这样我们的dbContexts就不会很大并且易于维护
#1
4
I went through this same process recently and found some great resources on the subject. Here are a couple that were very helpful:
我最近经历了同样的过程,并在这个主题上找到了一些很好的资源。这是一对非常有帮助的夫妇:
- Shrink EF Models with DDD Bound Contexts.
- 使用DDD绑定上下文缩小EF模型。
- How to decide on a lifetime for your ObjectContext.
- 如何确定ObjectContext的生命周期。
I was building a Desktop app, and I ended up using multiple contexts so that I could keep the lifetime tied to the module rather than the application. This has worked out very well for me, and I like that my DbContext
isn't swamped with DbSets
and is limited to the ones that are relevant for the current module.
我正在构建一个桌面应用程序,最后我使用了多个上下文,这样我就可以将生命周期与模块而不是应用程序联系起来。这对我来说非常好,我喜欢我的DbContext没有被DbSets淹没,并且仅限于与当前模块相关的那些。
In an ASP.NET MVC app, it is different since the DbContext
will only live as long as the request, and in those cases, I usually use a single DbContext
to simplify things unless the database is very large. With a large database, I would probably break it apart into multiple DbContexts
just to limit the overhead and the clutter, and keep things compartmentalized.
在ASP.NET MVC应用程序中,它是不同的,因为DbContext只会在请求时生存,在这种情况下,我通常使用单个DbContext来简化操作,除非数据库非常大。有了一个大型数据库,我可能会把它拆分成多个DbContexts,只是为了限制开销和杂乱,并保持分区。
#2
0
currently the EF hasnt been made to be broken down into diff dbContexts. Here a great talk about it
目前EF尚未被分解为diff dbContexts。这里有一个很好的谈论它
What we have done for this case that we have made a project diff from our MVC website just for the database generation and then have separate dbContexts for every requirement.
我们为这个案例做了什么,我们已经从我们的MVC网站创建了一个项目差异,只是为了生成数据库,然后为每个需求都有单独的dbContexts。
This way our dbContexts are never large and are easy to maintain
这样我们的dbContexts就不会很大并且易于维护