none
Exception: The query contains references to items defined on a different data context RRS feed

  • Question


  • I have a statement LINQ to SQL that throw the following exception with the message:
    "The query contains references to items defined on a different data context."

    I have found out that it happens when I have two sources of diferent table, but they are of the same data base.

    why does it happen?



    Monday, April 7, 2008 4:42 PM

Answers

  • The DataContext is a kind of offline cache and it keeps track of all changes done to objects under its control. That's the reason why you should work with one DataContext (for one database).

    NorthWindDataContext dc = new NorthWindDataContext();

    The query tells the context which objects to fetch from the database. It declares what to do, it doesn't execute the fetch operations. If you execute a query twice the objects already in the cache of the DataContext will not be overwritten. This preserves your changes.

    var qry1 = from c in dc.Customers

    from o in dc.Orders

    where c.CustomerID == o.CustomerID

    select new { c.CompanyName, o.OrderDate };

    Since the query just defines an IEnumerator but doesn't execute it, you have to use the IEnumerator to execute the query. This will happen if you loop over the query results or if you assign them to a collection (ToList, ToDictionary, ToArray or ToLookup).

    var list = qry1.ToList();

    The member of this list references the objects in the DataContext.

    If you update an object, the DataContext knows about the update.

    If you add an object, you must tell the DataContext that there is a new object by

    dc.Customers.InsertOnSubmit(newCustomer);

    If you delete an object, you must tell the DataContext that the object should be removed from the database by

    dc.Customers.DeleteOnSubmit(delCustomer);

    All the changes done, will be simply applied to your database by calling dc.SubmitChanges() (can throw exceptions).

     

    regards

    Philipp

    Tuesday, April 8, 2008 11:27 AM

All replies

  • Hi,

     

    I guess you have written something like this:

    NorthWindDataContext dc1 = new NorthWindDataContext();  // 1. DataContext

    NorthWindDataContext dc2 = new NorthWindDataContext();  // 2. DataContext

    var qry1 = from c in dc1.Customers

    from o in dc2.Orders

    where c.CustomerID == o.CustomerID

    select new { c.CompanyName, o.OrderDate };

    var list = qry1.ToList();

    The execution of qry1.ToList() causes this exception.

     

    You should use instead

    NorthWindDataContext dc = new NorthWindDataContext();

    var qry1 = from c in dc.Customers

    from o in dc.Orders

    where c.CustomerID == o.CustomerID

    select new { c.CompanyName, o.OrderDate };

    var list = qry1.ToList();

     

    regards

    Philipp

    Monday, April 7, 2008 5:42 PM
  • thanks very much.
    I have this problem because I traslate code in LINQ preview.
    If I update and  persist an object  of query in data base, if I always use the same datacontext, Doesn´t affect?
    Tuesday, April 8, 2008 6:51 AM
  • The DataContext is a kind of offline cache and it keeps track of all changes done to objects under its control. That's the reason why you should work with one DataContext (for one database).

    NorthWindDataContext dc = new NorthWindDataContext();

    The query tells the context which objects to fetch from the database. It declares what to do, it doesn't execute the fetch operations. If you execute a query twice the objects already in the cache of the DataContext will not be overwritten. This preserves your changes.

    var qry1 = from c in dc.Customers

    from o in dc.Orders

    where c.CustomerID == o.CustomerID

    select new { c.CompanyName, o.OrderDate };

    Since the query just defines an IEnumerator but doesn't execute it, you have to use the IEnumerator to execute the query. This will happen if you loop over the query results or if you assign them to a collection (ToList, ToDictionary, ToArray or ToLookup).

    var list = qry1.ToList();

    The member of this list references the objects in the DataContext.

    If you update an object, the DataContext knows about the update.

    If you add an object, you must tell the DataContext that there is a new object by

    dc.Customers.InsertOnSubmit(newCustomer);

    If you delete an object, you must tell the DataContext that the object should be removed from the database by

    dc.Customers.DeleteOnSubmit(delCustomer);

    All the changes done, will be simply applied to your database by calling dc.SubmitChanges() (can throw exceptions).

     

    regards

    Philipp

    Tuesday, April 8, 2008 11:27 AM