Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架6.0 FirstOrDefault()对象引用未设置为对象的实例_C#_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

C# 实体框架6.0 FirstOrDefault()对象引用未设置为对象的实例

C# 实体框架6.0 FirstOrDefault()对象引用未设置为对象的实例,c#,entity-framework,asp.net-mvc-4,C#,Entity Framework,Asp.net Mvc 4,突然我得到一个错误:对象引用没有设置为对象的实例。我使用的是Entity Framework 6.0,一切正常,突然一切停止了代码: List<Price> prices = db.Prices.ToList() ?? new List<Price>(); if ( prices != null && prices.Any() ) { // Price is a data model generated by edmx Price

突然我得到一个错误:对象引用没有设置为对象的实例。我使用的是Entity Framework 6.0,一切正常,突然一切停止了代码:

List<Price> prices = db.Prices.ToList() ?? new List<Price>();
if ( prices != null && prices.Any() )
{
      // Price is a data model generated by edmx
      Price price = prices.FirstOrDefault();
}
List prices=db.prices.ToList()??新列表();
if(prices!=null&&prices.Any())
{
//Price是由edmx生成的数据模型
Price价格=prices.FirstOrDefault();
}
谁知道会发生什么变化?我见过其他线程在它们的堆栈跟踪中有一瞥,但我的线程没有,堆栈跟踪:

at ASP._Page_Views_FAStock__VehiclePrices_cshtml.Execute() in c:\wamp\www\netauto.co.za\apcloud.co.za\ApCloud\ApCloud.SM.UI\Views\FAStock\_VehiclePrices.cshtml:line 96
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy()
   at System.Web.Mvc.WebViewPage.ExecutePageHierarchy()
   at System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage)
   at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance)
   at System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext viewContext, TextWriter writer)
   at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult)
   at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass1a.<InvokeActionResultWithFilters>b__17()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation)
在ASP.\u页面\u查看\u FAStock\u VehiclePrices\u cshtml.Execute()中的c:\wamp\www\netauto.co.za\apcloud.co.za\apcloud\apcloud.SM.UI\Views\FAStock\\u VehiclePrices.cshtml:第96行
在System.Web.WebPages.WebPageBase.ExecutePageHierarchy()中
在System.Web.Mvc.WebViewPage.ExecutePageHierarchy()中
位于System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext、TextWriter writer、WebPageRenderingBase起始页)
位于System.Web.Mvc.RazorView.RenderView(ViewContext-ViewContext、TextWriter-writer、对象实例)
位于System.Web.Mvc.BuildManagerCompiledView.Render(ViewContext、ViewContext、TextWriter)
在System.Web.Mvc.ViewResultBase.ExecuteSult(ControllerContext上下文)中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext ControllerContext,ActionResult ActionResult)
在System.Web.Mvc.ControllerActionInvoker.c_uuDisplayClass1A.b_uuu17()中
位于System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter筛选器、ResultExecutingContext预文本、Func`1 continuation)

您确定错误发生在
FirstOrDefault()的行上吗


Price
构造函数是否会抛出异常?

prices
不应为
null
。您是否检查了
db
是否有问题。可能这个对象是空的吗?代码永远不会“突然”停止工作。有些事情改变了,我们无法从你的展示中看出这一点。进行调试,请参阅以获取有关如何进行调试的提示。@xxMUROxx,为了更清楚起见,我已简化了问题,问题的关键在于调用列表中的FirstOrDefault()函数。@CodeCaster,我知道什么是NullReferenceException,如果您不理解这个问题,最好不要将此答案标记为重复!也许像我说的那样再读一遍这个问题,我已经看到其他人在EF6+上也有类似的问题!同样,你的问题中没有足够的信息来正确回答它。这不像“哦,是的,如果在同一个项目中使用scape,EntityFramework6会随机抛出NullReferenceException”,如果这是您想要的。至少显示完整的堆栈跟踪,并指出引发此异常的特定方法调用或属性访问。为了更清楚起见,我简化了这个问题,最简单的是调用列表上的FirstOrDefault()函数。@Morgs
Price
构造函数是否会引发异常?否,构造函数为空。。。
List<Price> prices = db.Prices.ToList() ?? new List<Price>();
Price price = prices.FirstOrDefault() ?? new Price() { Amount = 0 };