何时在实体框架中调用Dispose

何时在实体框架中调用Dispose

本文介绍了何时在实体框架中调用Dispose?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我正在使用Spring.Net for IoC。从ASP.Net文件调用服务对象,使用这些服务对象执行CRUD操作。例如,我有CustomerService在Customer表上执行所有CRUD操作。我使用实体框架,实体被注入..我的问题是我在哪里调用dispose方法?



据我从API文档中了解,除非我调用Dispose(),否则将无法收集垃圾!那么在哪里和如何做?



示例服务类:

  public class CustomerService 
{
public ecommEntities entities = {get; set;}

public bool AddCustomer(Customer customer)
{
try
{
entities.AddToCustomer(customer);
entities.SaveChanges();
返回true;
}
catch(异常e)
{
log.Error(创建新客户时发生错误+ e.Message + e.StackTrace);
返回false;
}
}

public bool UpdateCustomer(Customer customer)
{
entities.SaveChanges();
返回true;
}

public bool DeleteCustomer(客户客户)



我只是在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] =上下文;
}
}

它在请求开始时创建它,并处理结束。上下文取自Ninject的 HttpContext.Current.Items ,并注入我的服务对象。我不知道它是如何与Spring.Net一起使用的,而您创建 ObjectContext 的方法是什么,但是我希望这个答案有所帮助。如果Sprint.Net支持每个请求生命周期,并且不需要任何模块,那么它也可以照顾处理。在其他情况下,您可以这样做。


In my application I am making use of Spring.Net for IoC. The service objects are called from the ASP.Net files to perform CRUD operations using these service object. For example, I have CustomerService to do all CRUD operations on Customer table. I use entity framework and the entities are injected .. my question is where do I call the dispose method?

As far as I understood from the API documentations, unless I call Dispose() there is no guaranty it will be garbage collected! So where and how do I do it?

Example Service Class:

 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)
.
.
.

And I just create an object of CustomerService at the ASP partial class and call the necessary methods.

Thanks in advance for the best practice and ideas..

Regards,

Abdel Raoof

解决方案

I have HttpModule in my application, which takes care of creating and disposing of context:

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;
    }
}

It creates it at the beginning of request and disposes at the end. Context is taken from HttpContext.Current.Items by Ninject and injected into my service object. I don't know how exactly it works with Spring.Net and what is your approach to creating ObjectContext, but I hope this answer will help. If Sprint.Net supports per request lifetime and doesn't need any modules, it propably takes care of disposing too. In other case, you can do it like here.

这篇关于何时在实体框架中调用Dispose?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 00:18