简体   繁体   English

Scaffold-DbContext 失败。 System.TypeLoadException:无法在 .NET Core 上加载类型“Microsoft.EntityFrameworkCore.Internal.ProductInfo”

[英]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.我刚刚创建了一个 ASP .NET Core 3.1 API Web 应用程序,并想从我的 Oracle 数据库生成“DbContext”文件。 I am disappointed to find out that there seems to be no visual designer built in like there is in .NET Standard.我很失望地发现似乎没有像 .NET Standard 那样内置的视觉设计器。

What I have tried:我尝试过的:

  • Created .NET Core 3.1 API Web Application.创建了 .NET Core 3.1 API Web 应用程序。
  • Installed the following NuGet packages:安装了以下 NuGet 包:
    • Microsoft.EntityFrameworkCore 3.1.2; Microsoft.EntityFrameworkCore 3.1.2;
    • Microsoft.EntityFrameworkCore.Design 2.2.6; Microsoft.EntityFrameworkCore.Design 2.2.6;
    • Microsoft.EntityFrameworkCore.Relational 2.2.6; Microsoft.EntityFrameworkCore.Relational 2.2.6;
    • Microsoft.EntityFrameworkCore.Relational.Design 1.1.6 Microsoft.EntityFrameworkCore.Relational.Design 1.1.6
    • Microsoft.EntityFrameworkCore.Tools 2.2.6; Microsoft.EntityFrameworkCore.Tools 2.2.6;
    • Oracle.EntityFrameworkCore 2.19.6; Oracle.EntityFrameworkCore 2.19.6;
    • Oracle.ManagedDataAccess.Core 2.19.6; Oracle.ManagedDataAccess.Core 2.19.6;

I then tried calling the Scaffold-DbContext command on the package manager console:然后我尝试在包管理器控制台上调用Scaffold-DbContext命令:

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:使用此命令,我希望为我的表生成一个 db 上下文类,但是,返回以下错误:

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所有实体框架和 Microsoft.VisualStudio.Web.CodeGeneration.Design 包必须处于同一版本级别,例如 v3.1 以下

    <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.鉴于上述情况,TypeLoadException 应该消失,可能会被Instance Failure错误取代,比如您使用的连接字符串是否与您的项目使用的连接字符串相同。 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:正确的方式 - 注意下面 MSSQLSERVER17 之前的单斜杠:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在 EF Core 中加载类型“Microsoft.EntityFrameworkCore.Internal.ProductInfo”错误 - Could not load type 'Microsoft.EntityFrameworkCore.Internal.ProductInfo' error in EF Core ASP.NET Core错误:System.TypeLoadException:无法加载类型&#39;Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream&#39; - ASP.NET Core Error: System.TypeLoadException: Could not load type 'Microsoft.AspNetCore.WebUtilities.FileBufferingWriteStream' ASP.NET错误:System.TypeLoadException无法加载字段类型&#39;Microsoft.Owin.Security.ActiveDirectory - ASP.NET Error: System.TypeLoadException Could not load type of field 'Microsoft.Owin.Security.ActiveDirectory .Net核心支架dbcontext失败,无法加载类型&#39;System.IO.File&#39; - .Net core scaffold dbcontext fails with Could not load type 'System.IO.File' System.TypeLoadException:asp.net 核心中的“Microsoft.AspNetCore.Mvc.Razor.Internal.FileProviderRazorProjectFileSystem”类型中的“方法“GetItem” - System.TypeLoadException: 'Method 'GetItem' in type 'Microsoft.AspNetCore.Mvc.Razor.Internal.FileProviderRazorProjectFileSystem' in asp.net core Scaffold-DbContext 在 .net 核心中抛出错误“找不到程序集” - Scaffold-DbContext throws error “Could not find assembly” in .net core C# System.TypeLoadException:无法加载类型,因为它在偏移处包含 object 字段 - 来自 Z2D50972FECD376129545507F1062089Z 框架的代码 - C# System.TypeLoadException: Could not load type because it contains an object field at offset - code from .net framework on .net core 无法将“System.DBNull”类型的对象转换为 Scaffold-DbContext .Net Core 中的“System.String”类型 - Unable to cast object of type 'System.DBNull' to type 'System.String' in Scaffold-DbContext .Net Core System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2 - System.TypeLoadException Microsoft.VisualBasic ASP.NET Core 2 System.TypeLoadException:无法加载类型&#39;System.Func`2&#39; - System.TypeLoadException: Could not load type 'System.Func`2'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM