Luis Rosales
Luis Rosales

Reputation: 133

How to shim DbEntityEntry<TEntry> with Microsoft Fakes

I'm using Microsoft Fakes and I have tried to shim the object DBEntityEntry with System.Data.Entity.Infrastructure.Fakes.ShimDbEntityEntry but when I try to tell the shim to return a DbEntry object I can't because the DbEntry constructor is internal. What do I have to do to return a new DbEntry or just don't do nothing.

System.Data.Entity.
Infrastructure.Fakes
.ShimDbEntityEntry<RequisitionDetail>
.AllInstances.EntityGet = m => { DbEntityEntry<RequisitionDetail> r; };

I've tried to return null but when I try to change its state I get a null reference.

db.Entry(obj).State = EntityState.Modified;

How to isolate that dependency?

Upvotes: 1

Views: 313

Answers (1)

Jonathan Magnan
Jonathan Magnan

Reputation: 11347

You can access to the internal constructor via reflection.

var internalConstructor = typeof (DbEntityEntry).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
var dbEntityEntry = (DbEntityEntry)internalConstructor.Invoke(new object[] {null});

Upvotes: 1

Related Questions