Question

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?

1
6
3/14/2013 1:12:58 PM

Accepted Answer

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
7
3/14/2013 1:14:53 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow