C# 值不能为空。参数名称:key
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1959890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Value cannot be null.Parameter name: key
提问by Barbaros Alp
Value cannot be null.Parameter name: key
值不能为空。参数名称:key
I have started to get this error since i ve implemented the StructureMapControllerFactory as my DefaultControllerFactory.
自从我将 StructureMapControllerFactory 实现为我的 DefaultControllerFactory 以来,我开始收到此错误。
Actually i copied the code from MVC Sample App StoreFront, but i couldn't figure out why this error keeps popping up. Even this error show up the application still runs.
实际上,我从 MVC Sample App StoreFront 复制了代码,但我不明白为什么这个错误不断弹出。即使出现此错误,应用程序仍在运行。
What might be the reason of this error ?
这个错误的原因可能是什么?
Thanks for your time
谢谢你的时间
System.ArgumentNullException was unhandled by user code
Message="Value cannot be null.\r\nParameter name: key"
Source="mscorlib"
ParamName="key"
StackTrace:
at System.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
at System.Collections.Generic.Dictionary`2.ContainsKey(TKey key)
at StructureMap.Util.Cache`2.get_Item(KEY key)
at StructureMap.BuildSession.CreateInstance(Type pluginType)
at StructureMap.Container.GetInstance(Type pluginType)
at StructureMap.ObjectFactory.GetInstance(Type pluginType)
at Yacht.Web.Controllers.StructureMapControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) in D:\Documents\WebSites\JOBS\Yacht\Yacht.Web\Controllers\StructureMapControllerFactory.cs:line 16
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
InnerException:
and this is the StructureMapControllerFactory class
这是 StructureMapControllerFactory 类
public class StructureMapControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(System.Web.Routing.RequestContext requestContext, Type controllerType)
{
return ObjectFactory.GetInstance(controllerType) as Controller;
}
}
采纳答案by brianng
See anurse's answer to StructureMap error when invalid controller.
请参阅 anurse's answer to StructureMap error when invalid controller。
回答by revgum
Also be aware that if a model has properties defined which do not map to a database field then this error can occur. It was kind of a hassle to track this down, so I'll leave an answer on SO in case others run into the same problem.
另请注意,如果模型定义了未映射到数据库字段的属性,则可能会发生此错误。追踪这个问题有点麻烦,所以我会在 SO 上留下答案,以防其他人遇到同样的问题。
My fix was simple, include DataAnnotations:
我的修复很简单,包括 DataAnnotations:
using System.ComponentModel.DataAnnotations;
And then use the NotMapped annotation on the specific properties:
然后在特定属性上使用 NotMapped 注释:
[NotMapped]
public string SomeCustomProperty { get; set; }
回答by phil soady
For those Hunting for Why they get
对于那些寻找为什么他们得到的人
Value cannot be null.Parameter name: key
值不能为空。参数名称:key
unexplained exception in the bowels of EF. Check one of POCOs is NOT using an Unsupported type. I check for POCOs against this list Not every type can be mapped in Entity framework. As of EF 5.0
EF 肠道中无法解释的异常。检查其中一个 POCO 未使用不受支持的类型。我根据此列表检查 POCO 并非每种类型都可以在实体框架中映射。从 EF 5.0 开始
// MY TESTING WOULD Indicate SEE // NOT SUPPORTED comment
public const string Boolean = "System.Boolean";
public const string Byte = "System.Byte";
public const string ByteArray = "System.Byte[]";
public const string SByte = "System.SByte"; // NOT SUPPORTED
public const string Char = "System.Char"; // NOT SUPPORTED
public const string Decimal = "System.Decimal";
public const string Double = "System.Double";
public const string Single = "System.Single";
public const string Int32 = "System.Int32";
public const string UInt32 = "System.UInt32";// NOT SUPPORTED
public const string Int64 = "System.Int64";
public const string UInt64 = "System.UInt64";// NOT SUPPORTED
public const string Int16 = "System.Int16";
public const string UInt16 = "System.UInt16";
public const string String = "System.String";
public const string DateTimeOffset = "System.DateTimeOffset";
public const string DateTime = "System.DateTime";
public const string Guid = "System.Guid";
public const string Enum = "System.Enum";
public const string Type = "System.Type";// NOT SUPPORTED
This list is not exhaustive. But it does cover some important scalar types often used that May cause issues.
此列表并非详尽无遗。但它确实涵盖了一些经常使用的可能会导致问题的重要标量类型。
回答by MattC
Revgum's answer was exactly what I needed using VS2012 MVC and EF5. One minor change in EF5, the [NotMapped] decoration definition is in this library:
Revgum 的回答正是我使用 VS2012 MVC 和 EF5 所需要的。EF5 中的一个小变化,[NotMapped] 装饰定义在这个库中:
using System.ComponentModel.DataAnnotations.Schema;
(I believe his library works for EF4 and below but don't quote me on that)
(我相信他的库适用于 EF4 及以下版本,但不要引用我的话)
Also, keep in mind that if your model has a foreign key relationship, all child models can cause the same error to be thrown if their unmapped fields are not properly decorated.
另外,请记住,如果您的模型具有外键关系,并且所有子模型的未映射字段未正确修饰,则所有子模型都可能导致抛出相同的错误。
If you are using EF to reverse engineer code first and not touching the models, then this should never be an issue.
如果您首先使用 EF 对代码进行逆向工程而不涉及模型,那么这应该永远不会成为问题。