0
votes

So I recently installed Entity Framework Core 5.0 Preview 3 because I needed to use Filtered Include.

source : https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-5-0-preview-3/

Now I had to alter a table in my Database, and naturally, I executed this command in PackagaManagerConsole:

Scaffold-DbContext "Server=<Server Name>;Initial Catalog=<Database Name>;User ID=<user id>;Password=<password>;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Entity -Project <project Name> -force

usually it works fine and the DBContext is updated. But now I'm getting the following exception:

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.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)

I tried updating theses packages to the latest version :

Microsoft.EntityFrameworkCore ---> 3.1.8

Microsoft.EntityFrameworkCore.Design ---> 3.1.8

Microsoft.EntityFrameworkCore.Relational ---> 3.1.8

Microsoft.EntityFrameworkCore.SQLServer ---> 3.1.8

Microsoft.EntityFrameworkCore.Tools ---> 3.1.8

but I'm still getting the exception. what should I do? I like having Filtered Include, so I don't want to uninstall the latest efcore preview.

1

1 Answers

2
votes

So I went back and read the article that I referenced in the question, and I tried using developer command prompt instead of package manager console to scaffold the DBcontext. I executed this command :

 `dotnet ef dbcontext scaffold "server=<servername>;database=<database>;user=<user>;password=<password>" Microsoft.EntityFrameworkCore.SqlServ
er --project  "DataAccess" --output-dir "Models" --force`

and it worked without problems. but I still can't understand why that heppened.