简体   繁体   English

将Moq与EntityFramework graphdiff一起使用

[英]Using Moq with EntityFramework graphdiff

I have just added GraphDiff in an existing Entity Framework solution which is utilizing Moq framework for testing. 我刚刚在利用Moq框架进行测试的现有Entity Framework解决方案中添加了GraphDiff。 All my tests that are using Moq in insert and update methods are now failing since method _context.UpdateGraph throws following exception: System.NullReferenceException: Object reference not set to an instance of an object. 我的所有在插入和更新方法中使用Moq的测试现在都失败了,因为方法_context.UpdateGraph引发以下异常:System.NullReferenceException:对象引用未设置为对象的实例。 GraphDiff on GitHub https://github.com/refactorthis/GraphDiff GitHub上的GraphDiff https://github.com/refactorthis/GraphDiff

UpdateGraph extension method: https://github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs UpdateGraph扩展方法: https : //github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs

How should you hookup Moq with GraphDiff? 您应该如何用GraphDiff连接Moq?

We had this problem as well. 我们也有这个问题。 This is how we solved it. 这就是我们解决的方法。

So this is in the IContext interface: 因此,这是在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; }

This is in the base context: 这是在基本上下文中:

 public virtual T UpdateGraph<T>(T entity, Expression<Func<IUpdateConfiguration<T>, object>> mapping = null) where T : class, new()
        {
            return null;
        }

and

private ObjectContext ObjectContext
        {
            get { return (this as IObjectContextAdapter).ObjectContext; }
        }

And this is in the actual concrete context: 这是在实际的具体情况下:

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

and

private ObjectContext ObjectContext
        {
            get { return (this as IObjectContextAdapter).ObjectContext; }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM