Reputation: 163
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:
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.
Upvotes: 1
Views: 5604
Reputation: 737
Solved in two steps:
All entity framework and Microsoft.VisualStudio.Web.CodeGeneration.Design packages must be at the same version level, e.g. 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>
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
Upvotes: 3