EF 上下文单例适用于 ASP.NET?
我已在使用 EF 4.0 的 IIS 7.5 上部署了 MVC 应用程序。 EF 上下文封装在整个应用程序共享的单例中。我这样做是因为我认为应用程序需要一个上下文来管理所有会话中的 CRUD 功能。
这似乎工作正常,但有两个问题困扰着我:
我偶尔会在数据库中发现重复的记录。注意:我不更新记录,我将旧记录标记为已删除并将新记录写入数据库。
偶尔,EF 会丢失 SQL 连接,导致整个应用程序崩溃,迫使我回收应用程序池。
这些问题是否可能是由于共享上下文引起的? EF 的上下文不使用单例模式会更好吗?使用单例是否会为所有用户的所有 Ajax 请求造成单点故障?
感谢您的指导...
public class SharedNHREntitiesContext
{ 私有只读 NHREntities _context;
// Static members are lazily initialized.
// .NET guarantees thread safety for static initialization.
private static readonly SharedNHREntitiesContext instance = new SharedNHREntitiesContext();
// Make the constructor private to hide it.
// This class adheres to the singleton pattern.
private SharedNHREntitiesContext()
{
// Create the ObjectContext.
_context = new NHREntities();
}
// Return the single instance of the ClientSessionManager type.
public static SharedNHREntitiesContext Instance
{
get
{
return instance;
}
}
public NHREntities Context
{
get
{
return _context;
}
}
}
I've deployed an MVC App on IIS 7.5 that uses EF 4.0. The EF context is encapsulated in a singleton that is shared throughout the app. I did that because I thought that the app needed a single context to manage CRUD functions across all sessions.
This seems to work fine except I've got two issues that are plaguing me:
I find duplicate records in the database occasionally. Note: I don't update records, I mark old records as deleted and write a new record to the DB.
Occasionally, EF looses the SQL Connection causing the entire application to crash, forcing me to recycle the app-pool.
Is it likely that these issues are caused by sharing the context? Would it be better not to use the singleton pattern for EF's context? Does using a singleton create a single point of failure for all Ajax requests from all users?
Thanks for any guidance...
public class SharedNHREntitiesContext
{
private readonly NHREntities _context;
// Static members are lazily initialized.
// .NET guarantees thread safety for static initialization.
private static readonly SharedNHREntitiesContext instance = new SharedNHREntitiesContext();
// Make the constructor private to hide it.
// This class adheres to the singleton pattern.
private SharedNHREntitiesContext()
{
// Create the ObjectContext.
_context = new NHREntities();
}
// Return the single instance of the ClientSessionManager type.
public static SharedNHREntitiesContext Instance
{
get
{
return instance;
}
}
public NHREntities Context
{
get
{
return _context;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是为了跟进:我从代码中删除了单例 EF 上下文。自从我这样做以来,这一周内我没有遇到任何错误...所以,在 MVC 控制器的调用中,单例上下文似乎不是一个好主意。
话虽这么说,我仍然偶尔会收到重复的行,所以我仍然有一些不涉及单例的问题需要解决。
感谢所有帮助我解决这个问题的人。
Just to follow up: I removed the singleton EF context from the code. I have not had any errors in the week since I did that... so, it seems a singleton context is not a good idea across invocations of the MVC controller.
That being said, I am still getting occasional duplicate rows so I still have some issues to resolve that don't involve the singleton.
Thanks to all those who helped me resolve this.