6

DbContext

public class HaberPortalDB : DbContext
{
    public DbSet<Haberler> Haberler { get; set; }
    public DbSet<Kategoriler> Kategoriler { get; set; }
    public DbSet<Yazarlar> Yazarlar { get; set; }
}

public class Haberler
{
    public virtual int Id { get; set; }
    public virtual string Baslik { get; set; }
    public virtual string Aciklama { get; set; }
    public virtual string Icerik { get; set; }

    public virtual int YazarId { get; set; }
    public virtual Yazarlar Yazar { get; set; }

    public virtual int KategoriId { get; set; }
    public virtual Kategoriler Kategori { get; set; }
    public virtual ICollection<Resimler> Resimler { get; set; }
}

public class Kategoriler
{
    public virtual int Id { get; set; }
    public virtual string KategoriAdi { get; set; }
    public virtual string Aciklama { get; set; }

    public virtual ICollection<Haberler> Haberler { get; set; }
}

public class Yazarlar
{
    public virtual int Id { get; set; }
    public virtual string YazarAdi { get; set; }
    public virtual string Ozgecmis { get; set; }
    public virtual string Eposta { get; set; }

    public virtual ICollection<Haberler> Haberler { get; set; }
}

public class Resimler
{
    public virtual int Id { get; set; }
    public virtual string Url { get; set; }
    public virtual string Ad { get; set; }

    public virtual Haberler Haber { get; set; }
}

The scaffolding is generating following action methods

    //
    // GET: /Test/

    public ActionResult Index()
    {
        return View(db.Kategoriler.ToList());
    }

    //
    // GET: /Test/Details/5

    public ActionResult Details(int id = 0)
    {
        Kategoriler kategoriler = db.Kategoriler.Find(id);
        if (kategoriler == null)
        {
            return HttpNotFound();
        }
        return View(kategoriler);
    }

    //
    // GET: /Test/Create

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Test/Create

    [HttpPost]
    public ActionResult Create(Kategoriler kategoriler)
    {
        if (ModelState.IsValid)
        {
            db.Kategoriler.Add(kategoriler);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(kategoriler);
    }

    //
    // GET: /Test/Edit/5

    public ActionResult Edit(int id = 0)
    {
        Kategoriler kategoriler = db.Kategoriler.Find(id);
        if (kategoriler == null)
        {
            return HttpNotFound();
        }
        return View(kategoriler);
    }

    //
    // POST: /Test/Edit/5

    [HttpPost]
    public ActionResult Edit(Kategoriler kategoriler)
    {
        if (ModelState.IsValid)
        {
            db.Entry(kategoriler).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(kategoriler);
    }

    //
    // GET: /Test/Delete/5

    public ActionResult Delete(int id = 0)
    {
        Kategoriler kategoriler = db.Kategoriler.Find(id);
        if (kategoriler == null)
        {
            return HttpNotFound();
        }
        return View(kategoriler);
    }

    //
    // POST: /Test/Delete/5

    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(int id)
    {
        Kategoriler kategoriler = db.Kategoriler.Find(id);
        db.Kategoriler.Remove(kategoriler);
        db.SaveChanges();
        return RedirectToAction("Index");
    }

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }

There are break points for each method. Dispose() method is working after working of other methods.

How Dispose() method is fired for every method working?

jrummell
  • 41,300
  • 17
  • 110
  • 165
AliRıza Adıyahşi
  • 14,706
  • 23
  • 108
  • 189

1 Answers1

7

A few things to note:

  • The lifetime of your controller is only as long as each request.
  • Each request will execute one action method.
  • Dispose is called when the controller completes the request.

So, this is what happens during each request:

  1. Controller initialized
  2. DbContext initialized
  3. Action method executes
  4. Controller Dispose method executes
jrummell
  • 41,300
  • 17
  • 110
  • 165
  • +1.I got it, "controller dispose is called after every request". But how? Where is the firing method? We you `using statement` for this, but how does controller called dispose method? – AliRıza Adıyahşi Mar 14 '13 at 13:25
  • @AliRızaAdıyahşi The MVC framework calls the controller's dispose method. – jrummell Mar 14 '13 at 13:29
  • @Forty-Two It's necessary if you want to dispose immediately. Another option would be using a DI framework that controls the lifetime for you, such as [Unity.Mvc3](http://unitymvc3.codeplex.com/). – jrummell Mar 14 '13 at 13:31
  • So, why do we use `using-statement` for DbContextClass in controller class? I mean there are a lot of code examples, tutorials and articles with `using-statement`. – AliRıza Adıyahşi Mar 14 '13 at 13:32
  • You could wrap each usage of your DbContext in a using statement, but because each request only executes one action method, the way the code is written in your question will have almost the same lifetime anyway. Basically, it's the same solution with a different implementation. – jrummell Mar 14 '13 at 13:35
  • Not, fully understoond :), but thanks for replay. I think, I should understand "almost the same" – AliRıza Adıyahşi Mar 14 '13 at 13:42
  • When I use `using-statement`, In View I cant use for example `Categories.Post.First().Title` , because DbContext disposing before View calling. So only Categories class is initializing. But in this way DbContext is disposing after view loading... Thanks again, you give me tips for understand... – AliRıza Adıyahşi Mar 14 '13 at 14:04