Using Moq with EntityFramework graphdiff

entity-framework graphdiff moq

Question

I have just added GraphDiff in an existing Entity Framework solution which is utilizing Moq framework for testing. 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. GraphDiff on GitHub https://github.com/refactorthis/GraphDiff

UpdateGraph extension method: https://github.com/refactorthis/GraphDiff/blob/develop/GraphDiff/GraphDiff/DbContextExtensions.cs

How should you hookup Moq with GraphDiff?

1
1
5/15/2015 12:17:56 PM

Accepted Answer

We had this problem as well. This is how we solved it.

So this is in the IContext interface:

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; }
        }
1
9/4/2015 8:44:54 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow