dotnet / efcore

EF Core is a modern object-database mapper for .NET. It supports LINQ queries, change tracking, updates, and schema migrations.
https://docs.microsoft.com/ef/
MIT License
13.74k stars 3.18k forks source link

Exception has been thrown by the target of an invocation. #24910

Closed marcos-cruz closed 2 years ago

marcos-cruz commented 3 years ago

I have a solution that uses netcoreapp3.1 and EntityFrameworkCore 5.0.5. The solution has two database contexts: 1 for Identity and another for a bounded context of registrations.

To create the migrations of Identity I use the command below, and it runs very well.

PM> Add-Migration InitialCreate -Context IdentityContext -OutputDir Data \ Migrations \

But when I try to create the registration migrations I get the message.

PM> Add-Migration InitialCreate -Context RegistrationsContext -OutputDir Migrations\
Build started...
Build succeeded.
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.SemanticVersionComparer' from assembly 'Microsoft.EntityFrameworkCore, Version=5.0.5.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(IOperationReportHandler reportHandler, IDictionary args)
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(Type type, Object[] args)
   at Microsoft.EntityFrameworkCore.Tools.ReflectionOperationExecutor..ctor(String assembly, String startupAssembly, String projectDir, String dataDirectory, String rootNamespace, String language, String[] remainingArguments)
   at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor(String[] remainingArguments)
   at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0(String[] args)
   at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args)
   at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)
Exception has been thrown by the target of an invocation.
PM> Add-Migration InitialCreate -Context RegistrationsContext -OutputDir Migrations\

This is my csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.5" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.5">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
  </ItemGroup>

  <ItemGroup>
    <ProjectReference Include="..\..\Commons\Bigai.MangoApi.Commons.Infra.Data\Bigai.MangoApi.Commons.Infra.Data.csproj" />
    <ProjectReference Include="..\Bigai.MangoApi.Registrations.Bussiness.Application\Bigai.MangoApi.Registrations.Bussiness.Application.csproj" />
    <ProjectReference Include="..\Bigai.MangoApi.Registrations.Bussiness.Domain\Bigai.MangoApi.Registrations.Bussiness.Domain.csproj" />
  </ItemGroup>

</Project>

How do I find out what is wrong?

Include provider and version information

EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: netcoreapp3.1 Operating system: IDE: Visual Studio 2019 16.9.4

AndriySvyryd commented 3 years ago

This is likely caused by a version mismatch between the installed packages. Please create a small repro project for us to investigate further.

marcos-cruz commented 3 years ago

The problem has been identified. It took a while because the Entity Framework provides no clue of the problem.

As I said, my solution works with two different database contexts. In one project I was using entity framework version 5.0.5, and in another version 3.1.4. Leaving everything in version 3.1.4 worked very well.