使用Where linq查询搜索数据库时的.Net Core 2.1 EF Core Null参考异常

时间:2018-08-28 14:19:48

标签: .net-core entity-framework-core

我在这里有一个非常简单的查询:

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

问题是当它尝试返回时,它给了我NullReferenceException。我知道问题在于Where语句,因为如果我使用下面的语句,它可以正常工作。

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

我把事情转移了无济于事。 我在StackOverflow上发现了类似的问题:EF Core 2.1 Props inside where cause null reference,但是ToList()对我也不起作用。

任何人都知道发生了什么事吗?

编辑

错误:

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

我的引擎类:

    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 个答案:

答案 0 :(得分:1)

结果是我的Engine表具有CompanyId而不是Company。我要做的就是将其更改为:

var context = _context.Engine
            .Include(e => e.EngineType)
            .Where(e => e.CompanyId == user.CompanyId);