在GetAuditLogsAsync
方法中,我的代码:
var query = from auditLog in _auditLogRepository.GetAll()
join user in _userRepository.GetAll() on auditLog.UserId equals user.Id into userJoin
from joinedUser in userJoin.DefaultIfEmpty()
select new {AuditLog = auditLog, joinedUser.NickName};
var count = await query.CountAsync();
当我进行单元测试时,发生错误:
System.NullReferenceException : Object reference not set to an instance of an object.
at lambda_method(Closure , TransparentIdentifier`2 )
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Storage.Internal.InMemoryDatabase.<>c__DisplayClass8_0`1.<CompileAsyncQuery>b__0(QueryContext qc)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.CountAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken)
at ChiakiYu.AuditLogs.AuditLogAppService.<GetAuditLogsAsync>d__3.MoveNext() in G:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\src\ChiakiYu.Application\AuditLogs\AuditLogAppService.cs:line 64
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Abp.Threading.InternalAsyncHelper.<AwaitTaskWithPostActionAndFinallyAndGetResult>d__5`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at ChiakiYu.Tests.Auditing.AuditLogAppService_Tests.<Should_Get_Audit_Logs>d__2.MoveNext() in G:\ChiakiYu\ChiakiYu.DotNetCore\aspnet-core\test\ChiakiYu.Tests\Auditing\AuditLogAppService_Tests.cs:line 75
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
但是当我修改代码时:
var query = from auditLog in _auditLogRepository.GetAll()
select new {AuditLog = auditLog};
var count = await query.CountAsync();
没关系。
使用join
我该怎么办?谢谢。
答案 0 :(得分:0)
我知道,我是如此愚蠢。
当AuditLog.UserId
为空时,joinedUser
为空,joinedUser.NickName
写错了。