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.73k stars 3.18k forks source link

add-migration preview 9 throwing System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.ProductInfo' #17849

Closed mfitconsultants closed 2 years ago

mfitconsultants commented 5 years ago

when trying to run the add-migration command in the PMC is throwing the following error;

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.ProductInfo' from assembly 'Microsoft.EntityFrameworkCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. at Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(Object 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) at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor() at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute() at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.b__0() at Microsoft.DotNet.Cli.CommandLine.CommandLineApplication.Execute(String[] args) at Microsoft.EntityFrameworkCore.Tools.Program.Main(String[] args)

The command I am running is;

add-migration new-migration -o Data/Migrations

To add, I have the ef code in a seperate class library, below is the .csproj for info;

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

  <PropertyGroup>
    <TargetFramework>netcoreapp3.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.0.0-preview9.19423.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0-preview9.19423.6">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview9.19423.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite" Version="3.0.0-preview9.19423.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview9.19423.6">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="3.0.0-preview9.19423.4" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0-preview9.19423.4" />
  </ItemGroup>
</Project>

I run the migrations through a context factory as follows;

 public class MyContextFactory : IDesignTimeDbContextFactory<MyDataContext>
    {
        public MyContextFactory()
        {
        }

        public MyDataContext CreateDbContext(string[] args)
        {
            // Get environment
            string environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            // Build config
            IConfiguration config = new ConfigurationBuilder()
                .SetBasePath(Path.Combine(Directory.GetCurrentDirectory(), "../Blazor.Server"))
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment}.json", optional: true)
                .AddEnvironmentVariables()
                .Build();

            var connectionString = config.GetConnectionString($"{nameof(MyDataContext)}");

            var builder = new DbContextOptionsBuilder<MyDataContext>();

            builder.UseSqlServer(
                    connectionString,
                    x => x.UseNetTopologySuite());

            return new MyDataContext(builder.Options);
        }
    }

This was working fine up until I upgraded from 2.2.6.

chuckconway commented 5 years ago

I have the same error when trying to scaffold my database for the first time.

dotnet-ef dbcontext scaffold "Server=localhost;Database=Trust;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Schema

C:\Projects\Trust\Trust.Infrastructure>dotnet-ef dbcontext scaffold "Server=localhost;Database=Trust;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -o Schema
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
 ---> System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.ProductInfo' from assembly 'Microsoft.EntityFrameworkCore, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'.
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor..ctor(Object 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)
   at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
   at Microsoft.EntityFrameworkCore.Tools.Commands.DbContextScaffoldCommand.Execute()
   at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_0.<Configure>b__0()
   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.
luckyrat commented 5 years ago

The version of Microsoft.EntityFrameworkCore.Design needs to match Microsoft.EntityFrameworkCore and (I presume) the CLI.

Looks like that might already be the case for @mfitconsultants but perhaps it's worth doing a dotnet restore just in case the installed packages don't match that csproj file.

Once I had all versions matching, this error message went away for me. E.g.:

dotnet tool install --global dotnet-ef --version 3.0.0-preview9.19423.6
dotnet add package Microsoft.EntityFrameworkCore --version 3.0.0-preview9.19423.6
dotnet add package Microsoft.EntityFrameworkCore.Design --version 3.0.0-preview9.19423.6