简体   繁体   English

在存在Cache.insert的地方进行添加迁移或更新数据库失败

[英]Add-migration or update-database fails where Cache.insert exists

I am trying to run the add-migration command and get this error: 我正在尝试运行add-migration命令并出现以下错误:

PM> add-migration LatestModel
System.NullReferenceException: Object reference not set to an instance of an object.

which is erroring on this line of code: 这是在这行代码上的错误:

 public static string WebText(string key)
    {
        //Is value in cache?
        string outputValue;
        if (!CacheHelper.Get(key, out outputValue))
        {
            var str = StringsService.GetResource(value);

           HttpContext.Current.Cache.Insert(
           key,
           outputValue,
           null,
           System.Web.Caching.Cache.NoAbsoluteExpiration,
           TimeSpan.FromMinutes(60));

            return str;

        }

        return outputValue;
    }

Now, the function WebText just gets a value out of a database table, however, I cant see why entity framework is trying to scaffold or do anything with the HttpContect line. 现在,功能WebText只是从数据库表中获取一个值,但是,我看不到为什么实体框架尝试使用HttpContect行进行脚手架或执行任何操作。

error thrown 引发错误

System.NullReferenceException: Object reference not set to an instance of an object.
at PL_AnnexA_Application.Models.Helpers.WebtextHelpers.WebText(String key) in
Helpers.cs:line 36   
at System.ComponentModel.DisplayNameAttribute.GetHashCode()
at System.Collections.Generic.ObjectEqualityComparer`1.GetHashCode(T obj)
at System.Linq.Set`1.InternalGetHashCode(TElement value)
at System.Linq.Set`1.Find(TElement value, Boolean add)
at System.Linq.Enumerable.<ExceptIterator>d__99`1.MoveNext()
at System.Linq.Enumerable.<OfTypeIterator>d__aa`1.MoveNext()
Line 36 is

HttpContext.Current.Cache.Insert(

Any ideas? 有任何想法吗?

Looks like you are overriding a GetHashCode() method somewhere in an attribute and are calling your WebText method in it. 看起来您正在重写属性中某处的GetHashCode()方法,并在其中调用WebText方法。 The hashcode is probably used somewhere internally by EF when doing a migration. 进行迁移时,EF可能在内部使用哈希码。 And yes, your current http context will be NULL at that moment since it probably executes at the Application_Start in the global asax. 是的,此时您当前的http上下文将为NULL,因为它可能在全局asax的Application_Start中执行。

One solution is to remove your override of GetHashCode and solve your necessity for it elsewhere. 一种解决方案是删除对GetHashCode的覆盖,并在其他地方解决它的必要性。

The GetHashCode appears in your stacktrace. GetHashCode出现在您的堆栈跟踪中。

As I really need the cache functionalty and the displaytext override, I realised just check if HttpContect.Current is not null in the cache routine and that does the job. 因为我确实需要缓存功能和displaytext覆盖,所以我意识到只需检查HttpContect.Current在缓存例程中是否不为null即可。 I blame the 15 hours coding today. 我归咎于今天15个小时的编码。 thanks Anshul for the pointer. 感谢Anshul的指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM