.Net Core 2.1 EF Core Null Reference Exception when searching database with Where linq query

.net-core entity-framework-core

Question

I have a pretty simple query here:

public async Task<IActionResult> Index(string sortOrder)
    {
        ApplicationUser user = await _userManager.GetUserAsync(User);
        var context = _context.Engine
            .Include(e => e.EngineType)
            .Where(e => e.Company == user.Company);

        return View(await context.ToListAsync());
    }

The problem is it gives me a NullReferenceException when it tries to return. I know the problem is the Where statement because if I use the below it works fine.

public async Task<IActionResult> Index(string sortOrder)
    {
        ApplicationUser user = await _userManager.GetUserAsync(User);
        var context = _context.Engine
            .Include(e => e.EngineType);

        return View(await context.ToListAsync());
    }

I've shifted things around to no avail. I found this similiar issue on StackOverflow: EF Core 2.1 Props inside where cause null reference but ToList() doesn't work for me either.

Anyone know what's going on?

EDIT

The Error:

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

lambda_method(Closure , Company )
Microsoft.EntityFrameworkCore.Metadata.Internal.ClrPropertyGetter<TEntity, TValue>.GetClrValue(object instance)
Microsoft.EntityFrameworkCore.Storage.Internal.TypeMappedPropertyRelationalParameter.AddDbParameter(DbCommand command, object value)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalParameterBase.AddDbParameter(DbCommand command, IReadOnlyDictionary<string, object> parameterValues)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.CreateCommand(IRelationalConnection connection, IReadOnlyDictionary<string, object> parameterValues)
Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteAsync(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary<string, object> parameterValues, CancellationToken cancellationToken)
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)
System.Linq.AsyncEnumerable+SelectEnumerableAsyncIterator<TSource, TResult>.MoveNextCore(CancellationToken cancellationToken)
System.Linq.AsyncEnumerable+AsyncIterator<TSource>.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)
InstantBSI.Controllers.EnginesController.Index(string sortOrder) in EnginesController.cs
            return View(await context.ToListAsync());

My Engine class:

    public int Id { get; set; }
    public int CompanyId { get; set; }
    public Company Company { get; set; }
    [Display(Name = "Engine Type")]
    public int EngineTypeId { get; set; }
    public EngineType EngineType { get; set; }
    public List<Inspection> Inspections { get; set; }

    [Display(Name = "Serial Number")]
    public long SerialNumber { get; set; }
    [Display(Name = "License Number")]
    [DisplayFormat(NullDisplayText = "N/A")]
    public long LicenseNumber { get; set; }
    [Display(Name = "Total Flight Cycles")]
    public int TotalFlightCycles { get; set; }
    [Display(Name = "Total Flight Hours")]
    public int TotalFlightHours { get; set; }
    [Required]
    [Display(Name = "Aircraft Maintenence Manual")]
    public string AircraftMaintenenceManual { get; set; }
1
2
8/28/2018 2:42:13 PM

Accepted Answer

Turns out my Engine table had CompanyId instead of Company. All I had to do was change it to:

var context = _context.Engine
            .Include(e => e.EngineType)
            .Where(e => e.CompanyId == user.CompanyId);
2
8/28/2018 2:58:54 PM


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