0
votes

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

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
@CyrilDURAND please see first code block. NullReferenceException is thrown on CompiledQuery request - XpyM
does this: <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> say that I have EF5? - XpyM
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
@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