Entity Framework Patterns extension and Unit Test in Visual Studio 2012

Yesterday I was trying to make work simple test class in VS2012. I did not know what was wrong exactly. It was first project when I tried to glue together Entity Framework with repository pattern and Ninject as IoC container. Because of the fact that this was first time for solution like that I was sure that it is me, or rather source of my problem was indeed my code. But let’s not skip ahead.

So when I manage to translate adding EF classes to Unity from here: IoC : using EntityFramework.Patterns with Unity 2.1 into Ninject language. I got error like this from VS (when tried to running test case):

Result Message:    Unable to create instance of class UnitTest. Error: System.IO.FileLoadException: Could not load file or assembly 'EntityFramework, Version=4.3.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040).
Result StackTrace:    
at System.Signature.GetSignature(Void* pCorSig, Int32 cCorSig, RuntimeFieldHandleInternal fieldHandle, IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Signature..ctor(IRuntimeMethodInfo methodHandle, RuntimeType declaringType)
   at System.Reflection.RuntimeConstructorInfo.GetParametersNoCopy()
   at System.Reflection.RuntimeConstructorInfo.GetParameters()
   at Ninject.Injection.DynamicMethodInjectorFactory.EmitLoadMethodArguments(ILGenerator il, MethodBase targetMethod) in c:ProjectsNinjectninjectsrcNinjectInjectionDynamicMethodInjectorFactory.cs:line 115
   at Ninject.Injection.DynamicMethodInjectorFactory.Create(ConstructorInfo constructor) in c:ProjectsNinjectninjectsrcNinjectInjectionDynamicMethodInjectorFactory.cs:line 40
   at Ninject.Planning.Strategies.ConstructorReflectionStrategy.Execute(IPlan plan) in c:ProjectsNinjectninjectsrcNinjectPlanningStrategiesConstructorReflectionStrategy.cs:line 67
   at Ninject.Planning.Planner.<>c__DisplayClass1.<CreateNewPlan>b__0(IPlanningStrategy s) in c:ProjectsNinjectninjectsrcNinjectPlanningPlanner.cs:line 109
   at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action) in c:ProjectsNinjectninjectsrcNinjectInfrastructureLanguageExtensionsForIEnumerableOfT.cs:line 32
   at Ninject.Planning.Planner.CreateNewPlan(Type type) in c:ProjectsNinjectninjectsrcNinjectPlanningPlanner.cs:line 109
   at Ninject.Planning.Planner.GetPlan(Type type) in c:ProjectsNinjectninjectsrcNinjectPlanningPlanner.cs:line 71
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:ProjectsNinjectninjectsrcNinjectActivationProvidersStandardProvider.cs:line 77
   at Ninject.Activation.Context.Resolve() in c:ProjectsNinjectninjectsrcNinjectActivationContext.cs:line 157
   at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:ProjectsNinjectninjectsrcNinjectKernelBase.cs:line 386
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
   at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) in c:ProjectsNinjectninjectsrcNinjectPlanningTargetsTarget.cs:line 197
   at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) in c:ProjectsNinjectninjectsrcNinjectPlanningTargetsTarget.cs:line 165
   at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) in c:ProjectsNinjectninjectsrcNinjectActivationProvidersStandardProvider.cs:line 114
   at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target) in c:ProjectsNinjectninjectsrcNinjectActivationProvidersStandardProvider.cs:line 96
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Ninject.Activation.Providers.StandardProvider.Create(IContext context) in c:ProjectsNinjectninjectsrcNinjectActivationProvidersStandardProvider.cs:line 96
   at Ninject.Activation.Context.Resolve() in c:ProjectsNinjectninjectsrcNinjectActivationContext.cs:line 157
   at Ninject.KernelBase.<>c__DisplayClass10.<Resolve>b__c(IBinding binding) in c:ProjectsNinjectninjectsrcNinjectKernelBase.cs:line 386
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<CastIterator>d__b1`1.MoveNext()
   at System.Linq.Enumerable.Single[TSource](IEnumerable`1 source)
   at Ninject.ResolutionExtensions.Get[T](IResolutionRoot root, IParameter[] parameters) in c:ProjectsNinjectninjectsrcNinjectSyntaxResolutionExtensions.cs:line 37
   at UnitTest..ctor()

So my first thought was: heck, I probably did something wrong and it not so easy (Ninject registrations of components is really easy, you should try yourself). But I cannot find any references to that version of EF anywhere in my project. In fact I did not added them myself, everything was done by NuGet, so it should work, right? Yes. It should and it was working because:

1. Registration of classes from EntityFramework.dll was working perfectly.

2. Registration of classes from EntityFramework.Patterns.dll was not working at all.

3. When I added console project registration of both types from both assemblies was working without problems.

So the problem was with EntityFramework.Patterns.dll, its internal reference to older version of EF (I was using 5.0 version). Another problem was with assembly redirecting from older version of EF to newer 5.0. In console project section in app.config:

 <runtime>
    <assemblyBinding >
      <dependentAssembly>
        <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
      </dependentAssembly>
   </assemblyBinding>
  </runtime>

was doing what it suppose to, forcing application to use 5.0 version despite internal reference to version 4.3.1 inside EntityFramework.Patterns.dll. So maybe in test it is not present. Nope it was. Maybe configuration is not copied into Debug dir? Nope. Maybe it is named wrong? Nope. In this moment I had to take a few minutes off.

I was thinking: everything is as it suppose to. Assemblies, references, redirecting, configuration. So… maybe test environment is not starting correctly? Maybe it is ignoring that part of app.config. When I was adding NuGet packages same day I noticed update for VS. Why not tried that? Few clicks, about 1 hour and restart of VS after I tried again. Guess what? It worked!

So it was bug with VisualStudio. I was glad that it was over and little disappointed for few hours that I have lost. But it was at least one step closer in the right direction. 🙂

So if you encounter problem like that tried update VS 2012 with Update 2. It should work!

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
24 + 22 =