0

I have DataContext as a connection to my database and also use pre-compiled queries (CompiledQuery). Here is my code:

    public static RichTextValue GetRichTextValue(this DataManager db, string key, string lang = LobbyHelper.DefaultLanguage) {
        return CompiledQueryExtensions.RichTextValueByKeyAndLang(db, key, lang);//NullReferenceException is thrown here
    }

where DataManager extends DataContext. DataManager static instance is shared within the whole project.

public static class CompiledQueryExtensions
{
    public static Func<DataManager, string, string, RichTextValue> RichTextValueByKeyAndLang =
            CompiledQuery.Compile((DataManager db, string key, string lang) =>
                    db.RichTextValues.SingleOrDefault(x => !x.Deleted && !x.RichText.Deleted && !x.Language.Deleted
                             && x.RichText.Key == key && x.Language.Code == lang));
}

What most confuses me is that when page is requested ordinarily (via browser url) all is fine, but when I use ajax to request a PartialView and put it on the page, I get the excetpion.

Please assist

XpyM
  • 247
  • 4
  • 12
  • Which exception occurred and where, could we have more code sample ? By the way, since EF5, query are automatically compiled, if you use EF5+ you don't need CompiledQuery anymore. – Cyril Durand Feb 19 '15 at 09:04
  • @CyrilDURAND please see first code block. NullReferenceException is thrown on CompiledQuery request – XpyM Feb 19 '15 at 09:11
  • does this: `
    ` say that I have EF5?
    – XpyM Feb 19 '15 at 09:13
  • Sorry, didn't see the comment on the first code block. is db null ? if so, could you look at the stack trace and see why ? Your config file looks like you use EF5, if you care about performance, try to update to the latest version of EF, EF5 is 2.5+ year old and there were a lot of performance optimization since this version. – Cyril Durand Feb 19 '15 at 12:46
  • @CyrilDURAND no db and none of the other variables/objects are null. all of them are properly initialized. thank you for the tip, I will update my EF :) – XpyM Feb 20 '15 at 14:06

0 Answers0