views:

80

answers:

1

I am having a strange caching problem and I believe it might be related to the fact that I am using a Entity Data Model as my data source.

The problem is that I can update the database directly and it doesn't reflect on the actual site until I republish the project. The data does display properly when viewing the project locally.

The odd part is that it is only incorrect on the front end. If I view the record via the CMS, it shows properly.

Some source code:

// *** CMS CONTROLLER ***
[HandleError]
public class SiteManagerController : Controller
{
    static DataModel DB = new DataModel();
    // via CMS Controller    
    // CMS - Get the data *** PULLS DATA CORRECTLY
        public ActionResult Content()
        {
            List<SiteContent> viewData = DB.SiteContents.OrderBy(c => c.Title).ToList();
           return View(viewData);
        }

   // via CMS Controller
   // CMS - Update logic
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult ContentEdit(int ID, FormCollection formValues)
        {
            SiteContent siteContent = DB.SiteContents.Single(c => c.ID == ID);
            try
            {
                UpdateModel<SiteContent>(siteContent);
                DB.SaveChanges();
                return RedirectToAction("Content");
            }
            catch
            {
                throw;
            }
        }
  }

       // *** HOME CONTROLLER ***
       [HandleError]
       public class HomeController : Controller
       {
           static DataModel DB = new DataModel();
            [System.Web.Mvc.OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
            public ActionResult Index(string strSlug)
            {
                data = DietCenterDB.SiteContents.Single(c => c.Slug == strSlug);

                return View(data);
            }

       }

* UPDATE * It seems that there is some sort of timed delay associated with this issue. If I update the CMS/database everything functions properly after 15-20 minutes. Perhaps some object related to the front end data access needs to timeout or expire in order to grab the new content?

* UPDATE #2 * It seems that it's only the varchar/text fields that aren't updating properly. If I change an int field, it updates properly and the information is changed on every page.

A: 

Where is your 'DB' object being instantiated, and what is its scope? You should not be trying to cache that object anywhere; create a new one for each request and let it be GC'ed. That may be the source of your trouble, from the small bit of code here.

Andrew Barber
Updated original post to include more detail on how the DB object is being instantiated.
Don Boots
Ah hah; I think we have it here. You should not declare the DB object as static. Not only is that causing the unintended caching you are experiencing, the object in question is not thread safe, and is likely taking up a lot of resources over time.
Andrew Barber
Here's a link for more info: http://blogs.msdn.com/b/alexj/archive/2009/05/07/tip-18-how-to-decide-on-a-lifetime-for-your-objectcontext.aspx
Andrew Barber