Sono nuovo di asp.net mvc ef 7, cercando di migrare da vb.net.
Ho una classe come segue
[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; }
}
Ho generato un controller per questa classe, le operazioni CRUD funzionano correttamente se rimuovo System.DateTime pubblico Mod_DtTm {get; impostato; } colonna dalla classe, ma l'aggiunta di questa colonna dà un errore come mostrato in questa traccia Stack
Si è verificata un'eccezione non gestita durante l'elaborazione della richiesta.
NullReferenceException: riferimento oggetto non impostato su un'istanza di un oggetto.
lambda_method (Closure, ValueBuffer)
NullReferenceException: riferimento oggetto non impostato su un'istanza di un oggetto.
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
collezione1..ctor(IEnumerable
1)System.Linq.Enumerable.ToList [TSource] (IEnumerable`1 source)
FMWeb.Controllers.MatGrpsController.Index () in MatGrpsController.cs
- return View (_context.MatGrp.ToList ());
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
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 ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult ()
Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__53.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Mvc.Controllers.FilterActionInvoker.d__44.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Mvc.Infrastructure.MvcRouteHandler.d__6.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Routing.Template.TemplateRoute.d__27.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Routing.RouteCollection.d__9.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Builder.RouterMiddleware.d__4.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Authentication.AuthenticationMiddleware`1.d__18.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.ApplicationInsights.AspNet.ExceptionTrackingMiddleware.d__4.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.IISPlatformHandler.IISPlatformHandlerMiddleware.d__8.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Diagnostics.Entity.MigrationsEndPointMiddleware.d__5.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw ()
Microsoft.AspNet.Diagnostics.Entity.DatabaseErrorPageMiddleware.d__6.MoveNext ()
--- Fine della traccia dello stack dalla posizione precedente in cui è stata generata un'eccezione ---
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Attività dell'attività)
System.Runtime.CompilerServices.TaskAwaiter.GetResult ()
Microsoft.AspNet.Diagnostics.DeveloperExceptionPageMiddleware.d__7.MoveNext ()
Cosa sta andando male ?? come analizzare / eseguire il debug della traccia? Grazie in anticipo
La colonna Cre_DtTm consente null. di seguito è riportato lo script della tabella:
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]
Oh ho, ho avuto un errore stupido La colonna permette null nella definizione della tabella nel database, ma deve anche consentire null nella classe (? In DateTime? Per consentire null) DateTime pubblico? Cre_DtTm {get; impostato; }
Questo ha risolto il problema.
controlla il database. Se si tratta di una colonna NOT NULL e l'hai aggiunta senza usare un valore di default, diventerà tutta irreale