When running ef migrations script the following exception is thrown
dotnet ef migrations script --output c:/temp/x.Data/SQLMigrations.sql --context XxxContext --idempotent --project x.Data --startup-project x.Api
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.0.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.MigrationsScriptCommand.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)
Packages used in projects
<Project>
<ItemGroup>
<PackageReference Update="Microsoft.EntityFrameworkCore.SqlServer" Version="5.0.0-rc.1.20451.13" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Design" Version="5.0.0-rc.1.20451.13" />
<PackageReference Update="Microsoft.EntityFrameworkCore.Tools" Version="5.0.0-rc.1.20451.13" />
<PackageReference Update="Microsoft.EntityFrameworkCore.InMemory" Version="5.0.0-rc.1.20451.13" />
</ItemGroup>
</Project>
dotnet-tools.json:
{
"version": 1,
"isRoot": true,
"tools": {
"dotnet-ef": {
"version": "5.0.0-rc.1.20451.13",
"commands": [
"dotnet-ef"
]
}
}
}
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework:
dotnet --info
.NET SDK (reflecting any global.json):
Version: 5.0.100-rc.1.20452.10
Commit: 473d1b592e
Runtime Environment:
OS Name: Windows
OS Version: 10.0.19041
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100-rc.1.20452.10\
Host (useful for support):
Version: 5.0.0-rc.1.20451.14
Commit: 38017c3935
@kevbite I have not been able to reproduce this. Please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.
Think I've figured it out... seems we needed to add a Package Reference for Microsoft.EntityFrameworkCore.Design to our startup project too.
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.10.8" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.0-rc.1.*" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\xxx.Data\xxx.Data.csproj" />
</ItemGroup>
</Project>
I'm going to close this issue as it seems to be working alright now 馃憤 Thanks
@kevbite Did you still end up having to reference Microsoft.EntityFrameworkCore.Design from your startup project?
Yep, throws that exception if I don't include it.
@kevbite That reference shouldn't be needed. It would be great if you could post a project/solution that reproduces this so that we can investigate.
Sorry taken some time to get this together, I've cut down out project, however, now it seems to give an error advising to add the Microsoft.EntityFrameworkCore.Design packages, I'm not sure if something has updated itself since I first raised this issue but now it gives the below error when running migrations or database updates.
set SQLCONNSTR_DatabaseReadWrite=Server=.;Database=myDataBase;Trusted_Connection=True;
C:\dev\xxx.api>dotnet build
Microsoft (R) Build Engine version 16.8.0-preview-20451-02+51a1071f8 for .NET
Copyright (C) Microsoft Corporation. All rights reserved.
Determining projects to restore...
All projects are up-to-date for restore.
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
You are using a preview version of .NET. See: https://aka.ms/dotnet-core-preview
xxx.Data -> C:\dev\xxx.api\src\xxx.Data\bin\Debug\netstandard2.1\xxx.Data.dll
xxx.Api -> C:\dev\xxx.api\src\xxx.Api\bin\Debug\net5.0\xxx.Api.dll
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:02.49
C:\dev\xxx.api>cd src\xxx.Data
C:\dev\xxx.api\src\xxx.Data>dotnet ef migrations add NewMigration1 --startup-project ..\xxx.Api
Build started...
Build succeeded.
Your startup project 'xxx.Api' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
C:\dev\xxx.api\src\xxx.Data>dotnet ef database update --startup-project ..\xxx.Api
Build started...
Build succeeded.
Your startup project 'xxx.Api' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
```
@bricelam This repros for me when the design package is added as:
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.1.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Which is the default when using VS to add the package. If I add it as a regular package it works. Is this expected?
Your startup project 'xxx.Api' doesn't reference Microsoft.EntityFrameworkCore.Design. This package is required for the Entity Framework Core Tools to work. Ensure your startup project is correct, install the package, and try again.
This is the correct error. Do we have a repro for the TargetInvocationException?
Is this expected?
Yes, making the runtime assets private prevents it from flowing into the startup project. This is why we recommend installing Design directly into your startup project. You can also make it work by removing the <PrivateAssets> element.
Unfortunately, NuGet doesn't give us a way (from the package) to exclude the compile assets but still keep the runtime assets non-private. developmentDependency is too coarse-grained.
@bricelam So since most people will probably add the package like this, I think it means we should document that it is expected that you get this error unless you reference the package from your startup. (In other words, when I asked the question in triage whether this was expected, I think the answer should have been "yes".)
The TargetInvocationException wasn't expected--it should give a friendly error message telling you to install it into the startup project (as the repro does)
Ah, okay. That makes sense.
Hello, I'm having this exact same issues
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.0.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)
at Microsoft.EntityFrameworkCore.Tools.Commands.ProjectCommandBase.CreateExecutor()
at Microsoft.EntityFrameworkCore.Tools.Commands.MigrationsAddCommand.Execute()
at Microsoft.EntityFrameworkCore.Tools.Commands.CommandBase.<>c__DisplayClass0_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.
fixed i had to install recent core.design and sqlserver
I removed all packages and reinstalled them and build the project. It fixed my issue.
Most helpful comment
@kevbite That reference shouldn't be needed. It would be great if you could post a project/solution that reproduces this so that we can investigate.