[英]Using Moq with EntityFramework graphdiff
我刚刚在利用Moq框架进行测试的现有Entity Framework解决方案中添加了GraphDiff。 我的所有在插入和更新方法中使用Moq的测试现在都失败了,因为方法_context.UpdateGraph引发以下异常:System.NullReferenceException:对象引用未设置为对象的实例。 GitHub上的GraphDiff https://github.com/refactorthis/GraphDiff
UpdateGraph扩展方法: https : //github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs
您应该如何用GraphDiff连接Moq?
我们也有这个问题。 这就是我们解决的方法。
因此,这是在IContext接口中:
T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new();
DbEntityEntry<TEntity> Entry<TEntity>(TEntity entity) where TEntity : class;
DbEntityEntry Entry(object entity);
DbContextConfiguration Configuration { get; }
这是在基本上下文中:
public virtual T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new()
{
return null;
}
和
private ObjectContext ObjectContext
{
get { return (this as IObjectContextAdapter).ObjectContext; }
}
这是在实际的具体情况下:
public override T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) // where T : class, new()
{
return DbContextExtensions.UpdateGraph<T>(this, entity, mapping);
}
和
private ObjectContext ObjectContext
{
get { return (this as IObjectContextAdapter).ObjectContext; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.