简体   繁体   English

DbContext处理?

[英]DbContext disposing?

DbContext 的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. Dispose()方法在其他方法工作后正在工作。

How Dispose() method is fired for every method working? 如何为每个工作方法触发Dispose()方法?

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. 控制器完成请求时调用Dispose。

So, this is what happens during each request: 所以,这是在每个请求期间发生的事情:

  1. Controller initialized 控制器初始化
  2. DbContext initialized DbContext初始化
  3. Action method executes Action方法执行
  4. Controller Dispose method executes Controller Dispose方法执行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM