[RESOLVED]Stateless nature of ASP.Net MVC prevents default functionality of Entity Framework from working?

I am reading a book on ASP.NET MVC, where the author has mentioned that due to the stateless nature of ASP.NET MVC some of the features in Entity Framework are disabled.

While I understand the first part of this about MVC being stateless, I am confused about which parts of Entity Framework are not available.
Can someone help me understand which parts of Entity Framework get disabled when using it in ASP.NET MVC?

I write the following code in my controllers when using Entity Framework and it has never had a problem so far. My approach is to instantiate a private instance of DBContext for every controller and then use this instance in all my action methods, as shown
in code snippet below. Looking at my code below, the statement made by the author does not make any sense, but I may be missing some important fact.

public class StudentController : Controller
    {
        private ContosoUniversityEntities db = new ContosoUniversityEntities();

        // GET: /Student/
        public ActionResult Index()
        {
            return View(db.Students.ToList());
        }

        // GET: /Student/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Student student = db.Students.Find(id);
            if (student == null)
            {
                return HttpNotFound();
            }
            return View(student);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include="StudentID,LastName,FirstName,EnrollmentDate,MiddleName,CreateDate")] Student student)
        {
            if (ModelState.IsValid)
            {
                db.Students.Add(student);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(student);
        }
}

UPDATE 1:

The problem with using Entity Framework in web app is that its not possible to have an ObjectContext object live for the entire user session or even across postbacks for a page ( the first option would result in too much memory being used on server-side 
and second option would not be viable since ObjectContext object cannot be completely serialized).

Therefore, the part of Entity Framework that tracks changes to entity objects (i.e. ObjectServices) is unable to function as it was intended to when used in a web app.

So, the only solution we have for using Entity Framework in a web app is by instantiating a new ObjectContext object every time we need to perform any data operations against a database.

This solution is not a problem when adding a record or getting records in a web app using a new instance of ObjectContext since change tracking is automatically taken care of when adding an object using the normal framework API for adding entity and for
selecting records we need no change tracking. The problem is when we need to update a record or delete a record using a new instance of ObjectContext.

We can solve these problems in either of the two ways mentioned below.

  • Rely on Object Services for new ObjectContext: In this approach we need to retrieve the record we are updating or deleting and then use the regular API for updating/deleting, so Object Services can automatically decide the modified state/deleted
    state of the record. Note that this will involve an extra database trip to get the record we need to update.
using (var objContext = new MyDatabaseObjectContext())
{
    var customerToUpdate = (from c in objContext.Customers where c.customerid = 200).FirstOrDefault();
    if(customerToUpdate !=null)
     {
       customerToUpdate.SalesRep = "Michael Fox";
       customerToUpdate.CreditRating = 5;
       objContext.SaveChanges();
     }
}
  • Do not Rely on Object Services for new ObjectContext: We need to explicitly perform change tracking by setting the changed state of an entity object. Note that only one database trip is made using this approach.
using (var objContext = new MyDatabaseObjectContext())
{
       //let's say we already have an object customerToUpdate passed to our method
       customerToUpdate.SalesRep = "Michael Fox";
       customerToUpdate.CreditRating = 5;

       objContext.Entry(customerToUpdate).State = EntityState.Modified;
       objContext.SaveChanges();
     
}

they are not at all related. one is database access framework and one is web framework.

sun21170

stateless nature of ASP.NET MVC some of the features in Entity Framework are disabled.

Meaning for above statement may be like some of EF features or advantages not usable(not disabled) because of disconnect mode of http  

Leave a Reply