views:

54

answers:

1

Hello

I have had a problem where it appeared as though the second execution of a stored procedure was being ignored.

The first time I call my stored procedure it found 20 records and extracted information for the date range August 2009. The first line was meter id 233 with a data value 200

I then called the stored procedure and 20 records were also returned. This time meter id 233 had a data value of 300.

Linq is detecting that meterid 233 already exists in the context and so doesnt update

I am sure this is something to do with ObjectTrackingEnabled but this does not seem to be available for me to set to false?

I didnt write the context code so I dont really know how it all works, but I have noticed that it seems to inherit from ObjectDataContext

This was generate using Entity Framework and VS 2008

namespace DataServiceDAL {

/// <summary>
/// There are no comments for dbChildDataContext in the schema.
/// </summary>
public partial class dbChildDataContext : global::System.Data.Objects.ObjectContext
{
    /// <summary>
    /// Initializes a new dbChildDataContext object using the connection string found in the 'dbChildDataContext' section of the application configuration file.
    /// </summary>
    public dbChildDataContext() : 
            base("name=dbChildDataContext", "dbChildDataContext")
    {
        this.OnContextCreated();            
    }
    /// <summary>
    /// Initialize a new dbChildDataContext object.
    /// </summary>
    public dbChildDataContext(string connectionString) : 
            base(connectionString, "dbChildDataContext")
    {
        this.OnContextCreated();
    }
    /// <summary>
    /// Initialize a new dbChildDataContext object.
    /// </summary>
    public dbChildDataContext(global::System.Data.EntityClient.EntityConnection connection) : 
            base(connection, "dbChildDataContext")
    {
        this.OnContextCreated();
    }
    partial void OnContextCreated();

.............. }

I use the following linq to extract the data

    public static List<MeterTotalConsumpRecord> GetTotalAllTimesConsumption(DateTime dtStart, DateTime dtEnd, EUtilityGroup ug, int nMeterSelectionType, int nCustomerID, 
        int nUserID, string strSelection, bool bClosedLocations, bool bDisposedLocations)
    {    
        dbChildDataContext db = DBManager.ChildDataConext(nCustomerID);

        var tbl = from t in db.GetTotalConsumptionByMeter(dtStart, dtEnd, (int) ug, nMeterSelectionType, nCustomerID, nUserID, strSelection, bClosedLocations, bDisposedLocations, 1)                       
                  select t;                        

        return tbl.ToList();            
    }

I need a way of clearing this cache out so that the objects are updated properly or I need a way of refreshing the objects

Can anyone help?

Cheers

Paul

A: 

We recommend you to refresh objects using MergeOption. Here is a simple example:

var query = from d in context.Depts
                   where d.Deptno < 50
                   select d;
(query as ObjectQuery).MergeOption = MergeOption.OverwriteChanges;
Devart
Thanks for this - unfortunately I cannot test if this works because of the other problem I am getting http://stackoverflow.com/questions/2227685/query-results-cannot-be-enumerated-more-than-once do you have any ideas for that one?
Paul
Hi when I try this (query as ObjectQuery) is nullAny ideas?CheersPaul
Paul
Maybe you should try to use Entity SQL instead in this particular case. The reason is described here in Julie Lerman's blog:http://thedatafarm.com/blog/data-access/querying-with-linq-to-entities-vs-objectquery-in-ef/
Devart
How would I change my code to use Entity SQL Devart? I dont really understand that link
Paul
var query = new ObjectQuery<Dept>("select value dept from Depts as dept where dept.Deptno < 50", context);(query as ObjectQuery).MergeOption = MergeOption.OverwriteChanges;
Devart