NullReferenceException: Object reference not set to an instance of an object after adding DateTime column in ef7

asp.net entity-framework-core

Question

I am new to asp.net mvc ef 7, trying to migrate from vb.net.

I have a class as follows

[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; }

}

I have generated a controller for this class, CRUD opertions work fine if I remove public System.DateTime Mod_DtTm { get; set; } column from the class, but adding this column gives a error as show in this Stack trace

An unhandled exception occurred while processing the request.

NullReferenceException: Object reference not set to an instance of an object.

lambda_method(Closure , ValueBuffer )

NullReferenceException: Object reference not set to an instance of an object.

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.List1..ctor(IEnumerable1 collection)

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

FMWeb.Controllers.MatGrpsController.Index() in MatGrpsController.cs

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

--- End of stack trace from previous location where exception was thrown ---

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()

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

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

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()

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

--- End of stack trace from previous location where exception was thrown ---

System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)

System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

System.Runtime.CompilerServices.TaskAwaiter.GetResult()

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

What is going wrong?? how to analyze/debug the trace? Thanks in advance

The Column Cre_DtTm allows null. below is the table script:

 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]
1
-1
3/23/2016 10:05:09 AM

Accepted Answer

Oh ho, I got it a silly mistake The column allows null in table definition in database, but it must also allow null in the class (? in DateTime? to allow null) public DateTime? Cre_DtTm { get; set; }

This solved the problem.

7
3/15/2016 3:40:33 AM

Popular Answer

check the database. If it's a NOT NULL column and you added it without using a default value it'll get all ragey



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow