简体   繁体   中英

Scaffold-DbContext Fails. System.TypeLoadException: Could not load type 'Microsoft.EntityFrameworkCore.Internal.ProductInfo' on .NET Core

I have just created an ASP .NET Core 3.1 API Web Application and would like to generate 'DbContext' files from my Oracle database. I am disappointed to find out that there seems to be no visual designer built in like there is in .NET Standard.

What I have tried:

  • Created .NET Core 3.1 API Web Application.
  • Installed the following NuGet packages:
    • Microsoft.EntityFrameworkCore 3.1.2;
    • Microsoft.EntityFrameworkCore.Design 2.2.6;
    • Microsoft.EntityFrameworkCore.Relational 2.2.6;
    • Microsoft.EntityFrameworkCore.Relational.Design 1.1.6
    • Microsoft.EntityFrameworkCore.Tools 2.2.6;
    • Oracle.EntityFrameworkCore 2.19.6;
    • Oracle.ManagedDataAccess.Core 2.19.6;

I then tried calling the Scaffold-DbContext command on the package manager console:

Scaffold-DbContext "DATA SOURCE=(DESCRIPTION=(SOURCE_ROUTE=OFF)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=myHostName)(PORT=myPortNumber)))(CONNECT_DATA=(SID=mySID)(SERVER=DEDICATED))); User ID=myID;Password=myPassword;" Oracle.ManagedDataAccess -OutputDir Models -Tables myTableName

With this command, I was hoping a db context class would be generated for my table, however, the following error is returned instead:

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

Solved in two steps:

  1. All entity framework and Microsoft.VisualStudio.Web.CodeGeneration.Design packages must be at the same version level, eg v3.1 below

    <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include="Hangfire" Version="1.7.11" /> <PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.1" /> <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.1" /> <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1" /> </ItemGroup> </Project>
  2. Given the above, the TypeLoadException should go away, possibly replaced by an Instance Failure error, say if you are using the same connection string that your project uses. Solve the Instance Failure by using SINGLE backslashes in your connection string rather than the usual double:

WRONG WAY:

Scaffold-DbContext "Server=DESKTOP-C2IEMCJ\\MSSQLSERVER17;Database=HangFire01;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

RIGHT WAY - Notice the SINGLE slash right before MSSQLSERVER17 below:

Scaffold-DbContext "Server=DESKTOP-C2IEMCJ\MSSQLSERVER17;Database=HangFire01;Trusted_Connection=True" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM