0
votes

thanks for your time; a retired hobby developer needs help!

After I built, successfully, the MS tutorial 'AspNetCore.Docs-master --> ef-mvc --> cu-final' ...

... I want to add some fields for the 'Person (Student)' remark: the new fields do not have any relation to any other DB!

I.e. I added my fields to:

  • dbo.person (all went ok updating the db in VS2019)
  • models/student.cs (added)

    public string Phone { get; set; }   [just plain navchar(50) field]
    public string eMail { get; set; } [just plain navchar(50) field]
    public Decimal Cotisation { get; set; }  [decimal(18) field]
    public string Certificate { get; set; }  [just plain navchar(50) field]
    
  • changed accordingly views/Students/index.cshtml .... and the other .cshtml files remark: Build runs ok!

when running now all is ok except for the 'Students' tab, I get:

NullReferenceException: Object reference not set to an instance of an object. lambda_method(Closure , MaterializationContext )

ContosoUniversity.PaginatedList.CreateAsync(IQueryable source, int pageIndex, int pageSize) in PaginatedList.cs +41 var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();

when creating the sample I went through the 'inheritence' procedure to create the dbo.person DB. In VS2019, 'Sql Sever Object explorer' the dbo.person DB looks ok to me. I assumed just adding simple fields shouldn't be an issue BUT, as said, I get the nullreference lamda .. issue.

? do I need to go through another (inheritence) migration ? ! what little stpid thin do I miss ! I admit I don't have a clue what this lamda error is --> info attached!

thanks again for your time and help.

thanks, ed

Error msg:

An unhandled exception occurred while processing the request.
NullReferenceException: Object reference not set to an instance of an object.
lambda_method(Closure , MaterializationContext )
Stack Query Cookies Headers 
NullReferenceException: Object reference not set to an instance of an object.
lambda_method(Closure , MaterializationContext )
Microsoft.EntityFrameworkCore.Query.EntityLoadInfo.Materialize()
Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.GetEntity(IKey key, EntityLoadInfo entityLoadInfo, bool queryStateManager, bool throwOnNullKey)
Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.BufferedEntityShaper<TEntity>.Shape(QueryContext queryContext, ref ValueBuffer valueBuffer)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.BufferlessMoveNext(DbContext _, bool buffer, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteAsync<TState, TResult>(TState state, Func<DbContext, TState, CancellationToken, Task<TResult>> operation, Func<DbContext, TState, CancellationToken, Task<ExecutionResult<TResult>>> verifySucceeded, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable<T>+AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider+ExceptionInterceptor<T>+EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable.Aggregate_<TSource, TAccumulate, TResult>(IAsyncEnumerable<TSource> source, TAccumulate seed, Func<TAccumulate, TSource, TAccumulate> accumulator, Func<TAccumulate, TResult> resultSelector, CancellationToken cancellationToken) in Aggregate.cs
ContosoUniversity.PaginatedList<T>.CreateAsync(IQueryable<T> source, int pageIndex, int pageSize) in PaginatedList.cs
+
            var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
ContosoUniversity.Controllers.StudentsController.Index(string sortOrder, string currentFilter, string searchString, Nullable<int> pageNumber) in StudentsController.cs
+
            return View(await PaginatedList<Student>.CreateAsync(students.AsNoTracking(), pageNumber ?? 1, pageSize));
Microsoft.AspNetCore.Mvc.Internal.ActionMethodExecutor+TaskOfIActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, object controller, object[] arguments)
System.Threading.Tasks.ValueTask<TResult>.get_Result()

Table schema :

CREATE TABLE [dbo].[Person] (
    [Id]             INT            IDENTITY (1, 1) NOT NULL,
    [LastName]       NVARCHAR (50)  NOT NULL,
    [FirstName]      NVARCHAR (50)  NOT NULL,
    [Discriminator]  NVARCHAR (MAX) NOT NULL,
    [HireDate]       DATETIME2 (7)  NULL,
    [EnrollmentDate] DATETIME2 (7)  NULL,
    [Phone]          NVARCHAR (50)  NULL,
    [eMail]          NVARCHAR (50)  NULL,
    [Cotisation]     DECIMAL (18)   NULL,
    [Certificate]    NVARCHAR (50)  NULL,
    CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED ([Id] ASC)
);
1

1 Answers

1
votes

Make the Decimal column nullable to avoid that error:

In Student.cs, change to:

public Decimal? Cotisation { get; set; }