Mocking or faking DbEntityEntry or creating a new DbEntityEntry

Following my other question about the taunts of DbContext.Set I have another question about the taunts of EF Code First.

Now I have a method for my update that looks like this:

if (entity == null) throw new ArgumentNullException("entity"); Context.GetIDbSet<T>().Attach(entity); Context.Entry(entity).State = EntityState.Modified; Context.CommitChanges(); return entity; 

Context is the interface of my own DbContext.

The problem I'm working with is how I handle

Context.Entry(entity).State .

I went through this code and it works when I have a real live DbContext as an implementation of my Context interface. But when I put my fake context, I don’t know how to deal with it.

There is no constructor for the DbEntityEntry class, so I cannot just create a new one in my fake context.

Has anyone had any success with bullying or faking DbEntityEntry in your CodeFirst solutions?

Or is there a better way to handle state changes?

+54
entity-framework ef-code-first
Feb 17 '11 at 10:13
source share
1 answer

As in the other case, you need to add an additional level of indirection:

 interface ISalesContext { IDbSet<T> GetIDbSet<T>(); void SetModified(object entity) } class SalesContext : DbContext, ISalesContext { public IDbSet<T> GetIDbSet<T>() { return Set<T>(); } public void SetModified(object entity) { Entry(entity).State = EntityState.Modified; } } 

So, instead of invoking the implementation, you simply call SetModified .

+80
Feb 18 2018-11-11T00:
source share
β€” -



All Articles