EF

编程入门 行业动态 更新时间:2024-10-24 10:21:14
本文介绍了EF - 和存储库模式 - 多个上下文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在ASP.MVC2中的EF中面临一些上下文的麻烦。

我认为在DataBase中改进一些操作的最佳方法是创建了Repository。我的repo类添加,删除,选择很多项目,所以我不需要写

(使用< name> Context =新(...等)){...}

存储库消除了初始化上下文每个操作,但不要处理上下文。

什么是管理上下文的最佳方法?如果我创建其他存储库类,并尝试执行任何需要两个上下文中的对象的操作,都会出现问题。

是否有任何其他方式或更好的方式来实现存储库,管理上下文?任何有趣的模式?

解决方案

上下文是一个工作单位,所以每个Web请求都需要一个。

因此,您应该使用构造函数注入(即构造函数参数)为所有存储库提供单个上下文,并在请求结束时处理它。

大多数DI框架都会自动执行。

I've faced some troubles with context in EF in ASP.MVC2.

I thought that best way to improve some operation on DataBase i've created Repository. My repo class adds, deletes, select many items so i don't need to write

(using <name>Context = new (... etc ...) ) { ... }

Repository eliminates initializing context for every operation, but don't dispose the context.

What is the best way to manage contexts? If i create other repository class and try to do any operation which will need objects from both contexts there is a problem.

Is there any other way or better way to implement repository, to manage contexts? Any interesting pattern?

解决方案

A context is a unit of work, so you want one per web request.

Therefore, you should use constructor injection (i.e., a constructor argument) to supply a single context for all repositories, and dispose it at the end of the request.

Most DI frameworks will do this automatically.

更多推荐

EF

本文发布于:2023-05-26 16:23:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265256.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:EF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!