none
LINQ to SQL LoadWith bug? RRS feed

  • Question

  • Hi everyone,

    I'm working on an asp project at the moment and I ran into a few problems with LINQ.
    Unfortunately the part that's not working is pretty massive and I can't really post the code, but maybe someone has had a similar issue before and can point me in the right direction...

    I'm using a dataContext for the asp request lifetime. I have specified a number of loadWith options, which work fine in all pages except one, where LINQ just doesn't seem to honour it. I'm doing a simple query

    var values = from p in Db.FormValues select p;
    

    and have specified

    dlo.LoadWith<FormValues>(p=>p.Form);
    

    When the query runs I get all my formValues in values as expected, but the Form property is null for all of them. I inspect the actual SQL query and it's as you would expect -

    SELECT ... FROM FormValue INNER JOIN Form ON Form.FormId = FormValue.FormId

    So the query is correct - but it doesn't assign the values!?

    I tried a separate dataContext around this query, specified the same loadWith option, and it works! Exactly the same SQL query results but it actually populates the properties!

    I'm guessing something is stuffing up the datacontext... i'm running a fair few queries beforehand (not that much either, about 10-15 separate queries), but it still shouldn't happen, should it?

    Anyone ever have the same problem? Can't find anything similar so far...

    Thanks a lot!

    Cheers

    Thursday, September 9, 2010 6:49 AM

Answers

All replies

  • I'm guessing something is stuffing up the datacontext... i'm running a fair few queries beforehand (not that much either, about 10-15 separate queries), but it still shouldn't happen, should it?

    Well, do the queries you run pull data from formvalues? When the datacontext is created is there actual data there in Form?

    If so the problem is that you are getting cached data and creating a new datacontext is the easiest answer to your problems.

    Regards

    • Marked as answer by liurong luo Monday, September 13, 2010 9:31 AM
    • Unmarked as answer by Vince206 Tuesday, September 14, 2010 11:56 PM
    Thursday, September 9, 2010 2:17 PM
  • Hi Serguey123,

    thanks for the answer, and sorry for my late reply.

    I guess that makes sense... is there a way of disabling or clearing the cache though? I know the answer is probably to reduce the scope of the datacontext, but unfortunately that doesn't really work for my case...

    I have unmarked your reply as answer though because yes, there is data in formvalues before, but when it queries it the first time the same loadoptions exist so it should be cached with the attached objects, shouldn't it?? or does the cache "forget" links to other objects?

    Thanks for your help though, it made it a lot clearer to me.

    Cheers
    Vince

    Tuesday, September 14, 2010 11:56 PM
  • Hmm, you can refresh the datacontext as well if creating one new is too much of a problem

    http://msdn.microsoft.com/en-us/library/bb358706.aspx

    Also on how linq object tracking work

    http://msdn.microsoft.com/en-us/library/Bb386982(v=VS.100).aspx

    Perhaps this will make it clearer to you and help you in solving your problem :)

    Regards

    • Marked as answer by liurong luo Friday, October 15, 2010 2:09 AM
    Wednesday, September 15, 2010 1:01 PM
  • Make sure your load options are set when you create your datacontext.  Do not create your datacontext then do some queries then set your load options.
    • Marked as answer by liurong luo Friday, October 15, 2010 2:09 AM
    Wednesday, September 15, 2010 2:38 PM
  • Thanks again Serguey123, I'll give that a try. It works fine with another datacontext at the moment, but I'd probably like it better with just one...

    LitEnders, you can't change the load options after running a query. Throws exceptions.

    Cheers

    Wednesday, September 15, 2010 11:44 PM