Entity framework 何时在实体框架中调用Dispose?
在我的应用程序中,我将Spring.Net用于IoC。从ASP.Net文件调用服务对象,以使用这些服务对象执行CRUD操作。例如,我让CustomerService在Customer表上执行所有CRUD操作。我使用实体框架,实体被注入。。我的问题是在哪里调用dispose方法 据我从API文档中了解,除非调用Dispose(),否则不能保证它会被垃圾收集!那么我在哪里以及如何做呢 示例服务类:Entity framework 何时在实体框架中调用Dispose?,entity-framework,spring.net,Entity Framework,Spring.net,在我的应用程序中,我将Spring.Net用于IoC。从ASP.Net文件调用服务对象,以使用这些服务对象执行CRUD操作。例如,我让CustomerService在Customer表上执行所有CRUD操作。我使用实体框架,实体被注入。。我的问题是在哪里调用dispose方法 据我从API文档中了解,除非调用Dispose(),否则不能保证它会被垃圾收集!那么我在哪里以及如何做呢 示例服务类: public class CustomerService { public ecommEnt
public class CustomerService
{
public ecommEntities entities = {get; set;}
public bool AddCustomer(Customer customer)
{
try
{
entities.AddToCustomer(customer);
entities.SaveChanges();
return true;
}
catch (Exception e)
{
log.Error("Error occured during creation of new customer: " + e.Message + e.StackTrace);
return false;
}
}
public bool UpdateCustomer(Customer customer)
{
entities.SaveChanges();
return true;
}
public bool DeleteCustomer(Customer customer)
.
.
.
我只是在ASP部分类中创建CustomerService对象,并调用必要的方法
提前感谢您提供的最佳实践和想法
问候,
Abdel Raoof我的应用程序中有HttpModule,它负责创建和处理上下文:
public class MyEntitiesHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += ApplicationBeginRequest;
application.EndRequest += ApplicationEndRequest;
}
private void ApplicationEndRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Items[@"MyEntities"] != null)
((MyEntities)HttpContext.Current.Items[@"MyEntities"]).Dispose();
}
private static void ApplicationBeginRequest(Object source, EventArgs e)
{
//TextWriter logFile = File.CreateText(HttpContext.Current.Server.MapPath("~/sqllogfile.txt"));
var context = new MyEntities();
//context.Log = logFile;
HttpContext.Current.Items[@"MyEntities"] = context;
}
}
它在请求开始时创建它,并在请求结束时进行处理。Ninject从HttpContext.Current.Items中获取上下文,并将其注入到我的服务对象中。我不知道它在Spring.Net中是如何工作的,也不知道您是如何创建
ObjectContext
的,但我希望这个答案会有所帮助。如果Sprint.Net支持每个请求的生存期,并且不需要任何模块,那么它可能也会处理这些请求。在另一种情况下,您可以这样做。我的应用程序中有HttpModule,它负责创建和处理上下文:
public class MyEntitiesHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += ApplicationBeginRequest;
application.EndRequest += ApplicationEndRequest;
}
private void ApplicationEndRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Items[@"MyEntities"] != null)
((MyEntities)HttpContext.Current.Items[@"MyEntities"]).Dispose();
}
private static void ApplicationBeginRequest(Object source, EventArgs e)
{
//TextWriter logFile = File.CreateText(HttpContext.Current.Server.MapPath("~/sqllogfile.txt"));
var context = new MyEntities();
//context.Log = logFile;
HttpContext.Current.Items[@"MyEntities"] = context;
}
}
它在请求开始时创建它,并在请求结束时进行处理。Ninject从HttpContext.Current.Items中获取上下文,并将其注入到我的服务对象中。我不知道它在Spring.Net中是如何工作的,也不知道您是如何创建ObjectContext
的,但我希望这个答案会有所帮助。如果Sprint.Net支持每个请求的生存期,并且不需要任何模块,那么它可能也会处理这些请求。在另一种情况下,您可以这样做。@LukLed通常正确(+1)HTTP请求是正确的作用域。但是,我非常怀疑使用NInject或Spring.Net调用Dispose()
时是否需要显式代码,因为这两个应用程序都应该已经在处理它们所管理的实例HttpContext.Current
是存储引用的好地方
然而,你(@Abdel)说得不对:
据我从API文档中了解,除非调用Dispose(),否则不能保证它会被垃圾收集
这是不对的Dispose()
使集合更加优化,并提供非托管资源(例如,您的数据库连接)的确定性释放。但是,无论是否调用Dispose()
,最终都会收集所有内容 @LukLed通常正确(+1)HTTP请求是正确的作用域。但是,我非常怀疑使用NInject或Spring.Net调用Dispose()
时是否需要显式代码,因为这两个应用程序都应该已经在处理它们所管理的实例HttpContext.Current
是存储引用的好地方
然而,你(@Abdel)说得不对:
据我从API文档中了解,除非调用Dispose(),否则不能保证它会被垃圾收集
这是不对的
Dispose()
使集合更加优化,并提供非托管资源(例如,您的数据库连接)的确定性释放。但是,无论是否调用Dispose()
,最终都会收集所有内容 谢谢你的建议。。让我们看看别人怎么说谢谢你的建议。。让我们看看别人怎么说