This is a migrated thread and some comments may be shown as answers.

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

3 Answers 91 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
tango
Top achievements
Rank 1
tango asked on 23 Nov 2015, 03:00 PM

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.    }

3 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 26 Nov 2015, 04:48 PM
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.
0
tango
Top achievements
Rank 1
answered on 27 Nov 2015, 07:23 AM

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.

0
Ady
Telerik team
answered on 01 Dec 2015, 03:49 PM
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.
Tags
General Discussions
Asked by
tango
Top achievements
Rank 1
Answers by
Ady
Telerik team
tango
Top achievements
Rank 1
Share this question
or