Home

LinqPad8 is showing me a cached Entity ?

edited April 3

// This is my old Customer ID
var _oldCustomerID = "3ab4e2fb-5111-42d4-a0f2-388399c7d432";
var _newID = Guid.NewGuid().ToString();

// Here i simply display the Entity found using the old customer id
Customers.Single(s => s.CustomerIdInDataHost == _oldCustomerID).Dump();

// ASMX webservice that simply replace the customer id for the Entity (same database as used in my Query)
_dataHostService.ReplaceCustomerIdInDataHost(_datahostapikey, _owner, _newID, _oldCustomerID);

// Show the updated Customer with the new ID
Customers.Single(s => s.CustomerIdInDataHost == _newID).Dump();

Here's the problem: Both Dumps are IDENTICAL, which suggest that LinqPad is showing a cached entity ?
if i do a secondary Query and lookup the Customer , the CustomerID is correctly updated to the New ID

tech : MSSQL server 2017 (compat 110.. 2012)

Comments

  • I'm guessing this is Linq2Sql caching the entity.

    There are a number of ways to resolve this.

    If you are not going to be calling SubmitChanges() then you can disable the tracker and caching at the start of your query.

    this.ObjectTrackingEnabled = false;
    

    Alternatively keep a reference to the customer that will be changed outside LinqPad, and then refresh that record after calling your webservice - e.g.

    var customer = Customers.Single(s => s.CustomerIdInDataHost == _oldCustomerID).Dump();
    
    // ASMX webservice that simply replace the customer id for the Entity (same database as used in my Query)
    _dataHostService.ReplaceCustomerIdInDataHost(_datahostapikey, _owner, _newID, _oldCustomerID);
    
    // Refresh the customer record
     this.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, customer );
    
    // Show the updated Customer with the new ID
    Customers.Single(s => s.CustomerIdInDataHost == _newID).Dump();
    

    Alternatively you can create a new DataContext. Unfortunately you can do re-assign the new context to this, but you can something like

    var dc = new TypedDataContext(this.Connection.ConnectionString);
    
    dc.Customers.Single(s => s.CustomerIdInDataHost == _newID).Dump();
    

    That is kinda dangerous as it is very easy to forget to prefix your query with dc.

    Finally, Linq has a method to clear the cache but unfortunately it is marked as private, but you can if you use Uncapsulate, e.g.

    var customer = Customers.Single(s => s.CustomerIdInDataHost == _oldCustomerID).Dump();
    
    // ASMX webservice that simply replace the customer id for the Entity (same database as used in my Query)
    _dataHostService.ReplaceCustomerIdInDataHost(_datahostapikey, _owner, _newID, _oldCustomerID);
    
    this.Uncapsulate().ClearCache(); 
    
    // Show the updated Customer with the new ID
    Customers.Single(s => s.CustomerIdInDataHost == _newID).Dump();
    
Sign In or Register to comment.