EF linq 模拟中临时创建的表中的异常
Posted
技术标签:
【中文标题】EF linq 模拟中临时创建的表中的异常【英文标题】:Exception in Temporary created table in EF linq mocking 【发布时间】:2019-02-16 16:53:18 【问题描述】:我已经使用 Fake DB Context 实现了 EF 测试。下面是我的sql查询,
flatLicenseOrderId = (from users in this.CustomerEntities.UserMappers
where users.CompanyId == CompanyId && users.IsActive
join order in this.CustomerEntities.Orders on users.customerid equals order.customerid into orderDetails
from customerOrder in orderDetails.DefaultIfEmpty()
join license in this.CustomerEntities.License on users.Customerid equals license.Customerid into licenseDetails
from customerLicense in licenseDetails.DefaultIfEmpty()
from orderdetails in this.CustomerEntities.OrderDetails
where (customerOrder.OrderId == orderdetails.OrderId || customerLicense.OrderId == orderdetails.OrderId)
select orderdetails.OrderId).Distinct().ToList();
以下是我的模拟上下文,
public static CustomerEntities GetOrderDetails()
var mockData = new Mock<CustomerEntities>();
var users = new FakeDbSet<UserMapper>
new UserMapper CompanyId = 27835, IsActive = true, CustomerId = 956980 ,
new UserMapper CompanyId = 148150, IsActive = true, CustomerId = 1039733
;
mockData.Setup(m => m.UserMappers).Returns(users);
var order = new FakeDbSet<Orders>
new Orders CustomerId = 956980, OrderId = 401789 ,
new Orders CustomerId = 956980, OrderId = 426192
;
mockData.Setup(m => m.Orders).Returns(order);
var license = new FakeDbSet<License>
new License Customerid = 956980, OrderId = 401789 ,
new License Customerid = 1039733, OrderId = 423132
;
mockData.Setup(m => m.License).Returns(license);
var orderdetails = new FakeDbSet<OrderDetails>
new OrderDetails OrderId = 401789, ProductID = 7703 ,
new OrderDetails OrderId = 401789, ProductID = 7002
;
mockData.Setup(m => m.OrderDetails).Returns(orderdetails);
return mockData.Object;
在使用所有使用的表中存在的值模拟此代码时,模拟工作正常。但如果该值不在 Orders 表中,则会出现以下错误,
Message: Object reference not set to an instance of an object.
Source: Anonymously Hosted DynamicMethods Assembly
示例:对于 customerid = 956980,模拟工作正常,但对于另一个 customerid = 1039733,产生异常,因为客户在 Order 表中没有条目。
有人可以帮忙吗,如何处理此类情况的嘲笑?
【问题讨论】:
这是您不应尝试模拟 EF 的众多原因之一。将此作为集成测试,针对真实的 SQL 数据库。 【参考方案1】:试试这个。
在数据库操作的情况下它可能会失败,但对于模型,由于concise
操作,它会做得很好。
flatLicenseOrderId = (from users in this.CustomerEntities.UserMappers
.Where(x=>x.CompanyId==CompanyId && x.IsActive)
join order in this.CustomerEntities.Orders
on users.customerid equals order.customerid
into orderDetails
from customerOrder in orderDetails.DefaultIfEmpty()
join orderdetails in this.CustomerEntities.OrderDetails
on customerOrder?.OrderId equals orderdetails.OrderId
select newusers.customerid,orderdetails.OrderId)
.Distinct().ToList();
【讨论】:
在数据库操作的情况下它可能会失败 - 我们不能让我们的代码失败只是为了通过模型案例。我认为你的意思是正常的数据库操作将失败你建议的代码。以上是关于EF linq 模拟中临时创建的表中的异常的主要内容,如果未能解决你的问题,请参考以下文章
如何从另一个表中获取数据并插入到 EF Core 中所需的表中