.net core 升级到.net core 2后单元测试失败
也许有人能解释一下这意味着什么,为什么我会得到它 System.InvalidOperationException:从“VisitLambda”调用时, 重写“System.Linq.Expressions.ParameterExpression”类型的节点 必须返回相同类型的非空值。或者,覆盖 “VisitLambda”,并将其更改为不访问此类型的子级 我是从我的单元测试中得到的,我正在运行最新的.NETCore2和EFCore。我所有的测试都很好,直到我升级,然后我开始出错 有趣的是,当我运行这个项目时,如果它在测试中失败,那么这行代码就可以了 这是我的测试.net core 升级到.net core 2后单元测试失败,.net-core,entity-framework-core,.net-core-2.0,.net-standard-2.0,.net Core,Entity Framework Core,.net Core 2.0,.net Standard 2.0,也许有人能解释一下这意味着什么,为什么我会得到它 System.InvalidOperationException:从“VisitLambda”调用时, 重写“System.Linq.Expressions.ParameterExpression”类型的节点 必须返回相同类型的非空值。或者,覆盖 “VisitLambda”,并将其更改为不访问此类型的子级 我是从我的单元测试中得到的,我正在运行最新的.NETCore2和EFCore。我所有的测试都很好,直到我升级,然后我开始出错 有趣的是,当我运
[Fact]
public async Task GetUserProfileAsync_Where_Employee_Exist_Test()
{
// Given
var user = TestPrincipal.CreatePrincipalForEmployeeUser();
using (var factory = new TestContextFactory())
using (var context = factory.CreateInMemoryDatabase<ApplicationContext>())
{
this.SetDependencies(context);
var data = EmployeeValueHelper.GetEmployeeValues();
context.AddRange(data);
context.SaveChanges();
var sut = new ProfileService(new DbContextRepository<Data.Models.Employees.Employee>(context), this.userService, this.moqEmploymentStatusService.Object);
// When
// -> this method goes to a service and calls the below FindByIdAsync
var actual = await sut.GetProfileForUserAsync(user);
// Then
Assert.Equal(10, actual.EmployeeId);
}
}
public async Task<Employee> FindByIdAsync(long id)
{
var profile = await this.repository.Set
.Include(_ => _.Address) --> IT FAILS ON THIS LINE, IF I REMOVE THE INCLUDE THEN IT WORKS
.Include(_ => _.EmployeeImage)
.SingleOrDefaultAsync(_ => _.EmployeeId == id);
if (profile == null)
{
return null;
}
return profile;
}
[事实]
公共异步任务GetUserProfileAsync\u其中\u员工\u存在\u测试()
{
//给定
var user=TestPrincipal.CreatePrincipalForEmployeeUser();
使用(var factory=newtestcontextfactory())
使用(var context=factory.CreateInMemoryDatabase())
{
这个.SetDependencies(上下文);
var data=EmployeeValueHelper.GetEmployeeValues();
AddRange(数据);
SaveChanges();
var sut=new ProfileService(new DbContextRepository(context)、this.userService、this.moqEmploymentStatusService.Object);
//什么时候
//->此方法转到服务并调用下面的FindByIdAsync
var actual=await sut.GetProfileForUserAsync(用户);
//然后
断言.Equal(10,实际.EmployeeId);
}
}
公共异步任务FindByIdAsync(长id)
{
var profile=等待this.repository.Set
.Include(-->.Address)-->它在这一行失败,如果我删除Include,它就会工作
.Include(=>.EmployeeImage)
.SingleOrDefaultAsync(=>u0.EmployeeId==id);
if(profile==null)
{
返回null;
}
回报曲线;
}
更新
服务层
public class ProfileService : GenericService<Employee>, IProfileService
{
private readonly DbContextRepository<Employee> repository;
private readonly IUserService userService;
public ProfileService(DbContextRepository<Employee> repository, IUserService userService)
: base(repository)
{
this.repository = repository;
this.userService = userService;
}
public Task<Employee> GetProfileForUserAsync(ClaimsPrincipal user)
{
var id = this.userService.GetEmployeeId(user);
return id.HasValue ? this.FindByIdAsync(id.Value) : null;
}
public async Task<Employee> FindByIdAsync(long id)
{
var profile = await this.repository.Set
.Include(_ => _.Address)
.Include(_ => _.EmployeeImage)
.SingleOrDefaultAsync(_ => _.EmployeeId == id);
if (profile == null)
{
return null;
}
return profile;
}
}
公共类档案服务:GenericService、IProfileService
{
私有只读DbContextRepository存储库;
专用只读IUserService用户服务;
公共配置文件服务(DbContextRepository存储库,IUserService用户服务)
:base(存储库)
{
this.repository=存储库;
this.userService=userService;
}
公共任务GetProfileForUserAsync(ClaimsPrincipal用户)
{
var id=this.userService.GetEmployeeId(用户);
返回id.HasValue?this.FindByIdAsync(id.Value):null;
}
公共异步任务FindByIdAsync(长id)
{
var profile=等待this.repository.Set
.Include(=>u.Address)
.Include(=>.EmployeeImage)
.SingleOrDefaultAsync(=>u0.EmployeeId==id);
if(profile==null)
{
返回null;
}
回报曲线;
}
}
员工模式
public类雇员:IValidatableObject
{
[关键]
[列(“pkEmpID”)]
公共长雇员ID{get;set;}
[列(“fkCompanyID”)]
公共长公司ID{get;set;}
公共虚拟公司公司{get;set;}
[显示(Name=“lblEmpNumber”)]
公共字符串编号{get;set;}
公共虚拟IList地址{get;set;}=new List();
//还有一些不需要的东西
}
存储库
公共类DbContextRepository:IGenericRepository,IDisposable
地点:班级
{
公共DbContextRepository(ApplicationContext上下文)
{
this.Context=Context;
this.Set=context.Set();
this.SetWithNoTracking=this.Set.AsNoTracking();
}
公共应用程序上下文{get;}
公共数据库集集{get;}
不跟踪{get;}的公共IQueryable集
//还有一些不需要的东西
}
希望这将有助于了解更多信息您能为复制异常显示所有代码(EF实体、模型、存储库)吗?当然,让我更新我的question@TimurLemeshko完成,添加了一点信息。您能为复制异常显示所有代码(EF实体、模型、存储库)吗?当然,让我更新我的question@TimurLemeshko完成,添加了更多的信息
public class Employee : IValidatableObject
{
[Key]
[Column("pkEmpID")]
public long EmployeeId { get; set; }
[Column("fkCompanyID")]
public long CompanyId { get; set; }
public virtual Company Company { get; set; }
[Display(Name = "lblEmpNumber")]
public string EmpNumber { get; set; }
public virtual IList<Address> Address { get; set; } = new List<Address>();
// WITH SOME EXTRA STUFF NOT NEEDED FOR THIS
}
public class DbContextRepository<TEntity> : IGenericRepository<TEntity>, IDisposable
where TEntity : class
{
public DbContextRepository(ApplicationContext context)
{
this.Context = context;
this.Set = context.Set<TEntity>();
this.SetWithNoTracking = this.Set.AsNoTracking();
}
public ApplicationContext Context { get; }
public DbSet<TEntity> Set { get; }
public IQueryable<TEntity> SetWithNoTracking { get; }
// WITH SOME EXTRA STUFF NOT NEEDED FOR THIS
}