Efcore: Could not load file or assembly 'System.ComponentModel.Annotations'

Created on 24 Aug 2018  路  4Comments  路  Source: dotnet/efcore

Hello,
I had a project created using the 2.0 libraries which was working fine.
Although when I tried to run this project in some Windows machine where only the latest versions were installed (2.1) I got this error (after updating the project to the latest version as well).

I saw some resolutions but none of them work for me. I've tried to add bindingRedirect to my App.Config and to add the AutoGenerateBindingRedirects/GenerateBindingRedirectsOutputType to the .csproj. Same error still.

Is there any other possible fix?

Basically I have a data project targetting netstandard2.0 and net461 which I reference in a net461 test project where it will call some database methods.

If it helps, heres my csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <TargetFramework>net461</TargetFramework>
    <AssemblyName>MySchedules.Data</AssemblyName>
    <RootNamespace>MySchedules.Data</RootNamespace>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="2.1.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="Bogus" Version="22.0.8" />
    <PackageReference Include="SpecFlow" Version="2.3.2" />
  </ItemGroup>
  <PropertyGroup>
    <AutoGenerateBindingRedirects>True</AutoGenerateBindingRedirects>
    <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
  </PropertyGroup>
</Project>
Exception message:
Message: System.IO.FileNotFoundException : Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.


Stack trace:
at Microsoft.EntityFrameworkCore.Metadata.Conventions.Internal.CoreConventionSetBuilder.CreateConventionSet()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateConventionSet(IConventionSetBuilder conventionSetBuilder)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__1()
   at System.Lazy`1.CreateValue()
   at System.Lazy`1.LazyInitValue()
   at System.Lazy`1.get_Value()
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_Model()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()
   at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source, Expression`1 predicate)
   at MySchedules.Data.Repositories.EmployeeRepository.FindByFourthAccount(String fourthAccountIdlist) in C:\Users\itorres\Source\Repos\MySchedule.Automation\MySchedules.Data\Repositories\EmployeeRepository.cs:line 44
   at MySchedules.Data.Services.EmployeeService.GetEmployee(String fourthAccountId) in C:\Users\itorres\Source\Repos\MySchedule.Automation\MySchedules.Data\Services\EmployeeService.cs:line 19
   at MySchedules.Data.Steps.EmployeeDataSetupStep.GetEmployee() in C:\Users\itorres\Source\Repos\MySchedule.Automation\MySchedules.Data\Steps\EmployeeDataSetupStep.cs:line 26
   at lambda_method(Closure , IContextManager )
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioStart(ScenarioInfo scenarioInfo)
   at TechTalk.SpecFlow.TestRunner.OnScenarioStart(ScenarioInfo scenarioInfo)
   at MySchedules.FeaturesAndSteps.EmployeeRequest.CreateRequestsFeature.ScenarioSetup(ScenarioInfo scenarioInfo)
   at MySchedules.FeaturesAndSteps.EmployeeRequest.CreateRequestsFeature.DropAShift() in C:\Users\itorres\Source\Repos\MySchedule.Automation\MySchedules\Features and Steps\EmployeeRequest\CreateRequest.feature:line 5
--TearDown
   at MySchedules.Support.Hooks.AfterFeature() in C:\Users\itorres\Source\Repos\MySchedule.Automation\MySchedules\Support\Hooks.cs:line 40
   at lambda_method(Closure , IContextManager )
   at TechTalk.SpecFlow.Bindings.BindingInvoker.InvokeBinding(IBinding binding, IContextManager contextManager, Object[] arguments, ITestTracer testTracer, TimeSpan& duration)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.InvokeHook(IBindingInvoker invoker, IHookBinding hookBinding, HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireEvents(HookType hookType)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.FireScenarioEvents(HookType bindingEvent)
   at TechTalk.SpecFlow.Infrastructure.TestExecutionEngine.OnScenarioEnd()
   at TechTalk.SpecFlow.TestRunner.OnScenarioEnd()
   at MySchedules.FeaturesAndSteps.EmployeeRequest.CreateRequestsFeature.ScenarioTearDown()
Result Message: 
System.IO.FileNotFoundException : Could not load file or assembly 'System.ComponentModel.Annotations, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
TearDown : System.NullReferenceException : Object reference not set to an instance of an object.

Further technical details

EF Core version: 2.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system:
IDE: Visual Studio 2017 15.8.1

closed-question customer-reported

All 4 comments

Did You tried to clear/build Your project? Delete bin and obj folders and then rebuild the project again? I had the same error these days and this solved for me. Sometime re-add the lib in the project can solve too.

@Icaroto In addition to the suggestion above, you could also try clearing your NuGet package caches.

@Icaroto Another thing to test is removing the second TargetFramework--we are not sure whether multitargetting can be set this way, and VS also has issues with multitargetting in some situations.

Hi guys, really sorry the delay to get back.

I did some more investigation and found out that the issue is not with the data project (the one with the EF Core)

I added a few unit tests in this project and it worked fine. From that, I created a brand new net46 project to reference the data project again and it worked. So it was clear the issue was with the project referencing the data project.

I tried to compare line by line from the .csproj file from both (new and old) test project and couldn't find much (a few unnecessary packages but that was it)

Today I found out the issue: One of the NuGet packages my test solution has Polly. Turns out the version 6 or above add some .netstandard references (1.1 and 2.0) that conflicts with the ones EF Core uses (at least is what I think). Once I downgraded this package to previous version (5.9.0) worked fine.

Thanks all for the help!

Was this page helpful?
0 / 5 - 0 ratings