NullReferenceException:在ef7中添加DateTime列後,對象引用未設置為對象的實例

asp.net entity-framework-core

我是asp.net mvc ef 7的新手,試圖從vb.net遷移。

我有一個課程如下

[Table("MatGrp")]
public class MatGrp
{
    [Key]
    public decimal Grp_RNo { get; set; }

    [Required]
    [MaxLength(30)]
    [Display(Name = "Group Code")]
    public string Grp_Code { get; set; }

    [Required]
    [MaxLength(100)]
    [Display(Name = "Group Name")]
    public string Grp_Name { get; set; }
    public decimal Cre_By { get; set; }
    public DateTime Cre_DtTm { get; set; }
    public decimal Mod_By { get; set; }
    [DataType(DataType.DateTime)]
    public System.DateTime Mod_DtTm { get; set; }

}

我為這個類生成了一個控制器,如果我刪除公共System.DateTime Mod_DtTm {get;組;來自該類的列,但添加此列會在此堆棧跟踪中顯示錯誤

處理請求時發生未處理的異常。

NullReferenceException:未將對象引用設置為對象的實例。

lambda_method(Closure,ValueBuffer)

NullReferenceException:未將對象引用設置為對象的實例。

lambda_method(Closure,ValueBuffer)

Microsoft.Data.Entity.Query.EntityLoadInfo.Materialize()

Microsoft.Data.Entity.Query.Internal.QueryBuffer.GetEntity(IKeyValue keyValue,EntityLoadInfo entityLoadInfo,Boolean queryStateManager)

Microsoft.Data.Entity.Query.ExpressionVisitors.RelationalEntityQueryableExpressionVisitor.CreateEntity [TEntity](IQuerySource querySource,ValueBuffer valueBuffer,Int32 valueBufferOffset,QueryContext queryContext,IEntityType entityType,Boolean trackingQuery,KeyValueFactory keyValueFactory,Func`2 materializer,Boolean allowNullResult,Boolean useQueryBuffer)

lambda_method(Closure,ValueBuffer)

System.Linq.Enumerable。<> c__DisplayClass7_0`3.b__0(TSource x)

System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()

Microsoft.Data.Entity.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()

System.Collections.Generic.List 1..ctor(IEnumerable 1 collection)

System.Linq.Enumerable.ToList [TSource](IEnumerable`1 source)

MatGrpsController.cs中的FMWeb.Controllers.MatGrpsController.Index()

  1. return View(_context.MatGrp.ToList());

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Mvc.Controllers.ControllerActionExecutor.ExecuteAsync(MethodInfo actionMethodInfo,Object instance,Object [] orderedActionArguments)

Microsoft.AspNet.Mvc.Controllers.ControllerActionInvoker.d__6.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()

Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__53.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Routing.Template.TemplateRoute.d__27.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Routing.RouteCollection.d__9.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.ApplicationInsights.AspNet.ExceptionTrackingMiddleware.d__4.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.d__5.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext()

---從拋出異常的先前位置開始的堆棧跟踪結束---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任務任務)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext()

怎麼了?如何分析/調試跟踪?提前致謝

Column Cre_DtTm允許null。下面是表腳本:

 CREATE TABLE [dbo].[MatGrp](  [Grp_RNo] [numeric](5, 0) NOT NULL,  
 [Grp_Code] [varchar](50) NOT NULL,  [Grp_Name] [varchar](50) NOT NULL,
 [Cre_By] [numeric](3, 0) NOT NULL,  [Cre_DtTm] [datetime] NULL, 
 [Mod_By] [numeric](3, 0) NOT NULL,  [Mod_DtTm] [datetime] NULL, 
 CONSTRAINT [PK_MatGrp_Grp_RNo] PRIMARY KEY CLUSTERED  ( [Grp_RNo] ASC
 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY =
 OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON
 [PRIMARY]

一般承認的答案

呵呵,我搞錯了這個列在數據庫中的表定義中允許null,但它還必須允許類中的null(?在DateTime?中允許為null)public DateTime? Cre_DtTm {get;組; }

這解決了這個問題。


熱門答案

檢查數據庫。如果它是一個NOT NULL列並且您在不使用默認值的情況下添加它,那麼它將變得非常脆弱



Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow