difference between context.dispose() and context.cache.releaseall()

4 posts, 0 answers
  1. tango
    tango avatar
    5 posts
    Member since:
    Oct 2014

    Posted 23 Nov 2015 Link to this post

    Dear Telerik Team,

    I am a bit confused and wanted to know technically what exactly happens when i call each of these command, also which is preferable over another.

    My scenario is in desktop application, where there is a LoginForm, in which:

    1. User Logs In by giving username, password.

    2. I changed password directly in the database against current user.

    3. User Logs out.

    4. User Logs in again by giving username and new password but context is still holding old password.

    PROBLEM STATEMENT: HOW TO GET REFRESH DATA, BELOW IS MY CODE SNIPPED

     

    01.public partial class LoginForm : Form
    02.{
    03.    EntitiesModel _context = null;
    04.    public LoginForm()
    05.    {
    06.        InitializeComponent();
    07.    }
    08.    private void LoginForm_Load(object sender, EventArgs e)
    09.    {
    10.        _context = new EntitiesModel(Global.ConnectionString);
    11.    }
    12.    private void btnLogin_Click(object sender, EventArgs e)
    13.    {
    14.        USER user = _context.USERs.FirstOrDefault(u => u.USERNAME == txtUsername.Text.Trim() && u.PASSWORD == txtPassword.Text.Trim());
    15. 
    16.        // after authentication show main menu etc
    17.    }
    18.    private void btnLogout_Click(object sender, EventArgs e)
    19.    {
    20.        //option 1: i dispose off current context here and create new context each time login button is clicked, so that context fetches latest password from database
    21.        if (_context != null)
    22.             _context.Dispose();
    23. 
    24.        //option 2: i only release all entities from current context cache and use the same context each time login button is clicked rather creating new context
    25.        _context.cache.ReleaseAll();
    26.    }

  2. Ady
    Admin
    Ady avatar
    588 posts

    Posted 26 Nov 2015 Link to this post

    Hi muhammad,

     Does the second time you execute the query return you the same instance of USER? This should not happen. Have you enabled the second level cache? If second level cache is enabled the query is not executed against the database but the instance is returned from the cache.
    Please turn of the 2nd level cache or evict the cache in code.

    Hope this helps.

    Regards,
    Ady
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
  3. DevCraft banner
  4. tango
    tango avatar
    5 posts
    Member since:
    Oct 2014

    Posted 27 Nov 2015 in reply to Ady Link to this post

    Hi Sir,

    Yes i am getting same result and not updated one from database unless either i dispose context or release 1st level cache explicitly on log-off.

    Moreover 2nd level cache is already disabled, i have checked it by right clicking entity designer > show model settings > Backend configuration.

    Sir you can re-verify the same problem by using my code snippet, its already a working code and i am using latest Telerik OpenAccess.

  5. Ady
    Admin
    Ady avatar
    588 posts

    Posted 01 Dec 2015 Link to this post

    Hi muhammad,
     
     I am attaching a simple sample application that simulates the actions you have described. 
    I add an object to the database, then I fetch it using another context and modify it.
    I then fetch the object using the first context and successfully obtain the updated values.
    Please go through the sample and compare it against how you are handling the context instances.

    Do get back in case you need further assistance.

    Regards,
    Ady
    Telerik
     
    Check out the latest announcement about Telerik Data Access vNext as a powerful framework able to solve core development problems.
Back to Top