0

I'm attempting to scaffold an existing Azure database for automatically generating the context/model files in an MVVM scenario in a .NET 6, WINUI 3 project.

I'm using entityframeworkcore.tools v6.0.12 and the Package Manager Console for running the Scaffold-DBContext command. It looks something like this:

Scaffold-DbContext "Server=tcp:Server.database.windows.net, 1433;Initial Catalog=ProjectName;User ID=UserID;Password=Password;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;" 
         Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

The project builds during the scaffolding, but I'm receiving this error after that:

Failed to load the dll from [C:\Users\ebagby\Desktop\Temp\DbManagerTempModel\DbManagerTempModel\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86\hostpolicy.dll], HRESULT: 0x800700C1 An error occurred while loading required library hostpolicy.dll from [C:\Users\ebagby\Desktop\Temp\DbManagerTempModel\DbManagerTempModel\bin\x86\Debug\net6.0-windows10.0.19041.0\win10-x86]

I've scoured numerous posts/web pages with similar errors, but they don't seem to apply. Most discuss trying to run 32-bit .net but loading a 64-bit hostpolicy.dll. As you can see from the error path, I am loading a 32-bit .dll

From my .csproj file, in case it helps:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>WinExe</OutputType>
    <TargetFramework>net6.0-windows10.0.19041.0</TargetFramework>
    <TargetPlatformMinVersion>10.0.17763.0</TargetPlatformMinVersion>
    <RootNamespace>DbManagerTempModel</RootNamespace>
    <ApplicationManifest>app.manifest</ApplicationManifest>
    <Platforms>x86;x64;arm64</Platforms>
    <RuntimeIdentifiers>win10-x86;win10-x64;win10-arm64</RuntimeIdentifiers>
    <PublishProfile>win10-$(Platform).pubxml</PublishProfile>
    <UseWinUI>true</UseWinUI>
    <EnableMsixTooling>true</EnableMsixTooling>
    <PlatformTarget>x86</PlatformTarget>
  </PropertyGroup>

  <ItemGroup>
    various items...
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.12" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.12">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.WindowsAppSDK" Version="1.2.221209.1" />
    <PackageReference Include="Microsoft.Windows.SDK.BuildTools" Version="10.0.22621.755" />
    <Manifest Include="$(ApplicationManifest)" />
  </ItemGroup>
...

There's not a lot of documentation that is specifically on scaffolding azure databases with .NET 6/ WIN UI 3. Most examples are sqlite, ASP.NET, XAMARIN, and so on. Perhaps I'm missing a package for Azure to bring this all together? Don't think so, but I'm grasping at straws at this point. Any help is appreciated to get this scaffolding to work.

Edit: To download the entire project, go here: https://superstructures1.sharepoint.com/:u:/s/Teams-Studio-IT/ERGX16koClJGhyIyVZRVb64BmUyrvsq9Cv7fLMMLudI_5g?e=lbM6Xs

E. A. Bagby
  • 824
  • 9
  • 24
  • 1
    You should post your project somewhere so we can try to get the same issue. – Simon Mourier Jan 03 '23 at 21:16
  • It's a brand new project I'm using to try to get the scaffolding to work, so nothing terribly special there. If I post it, you wouldn't have access to my Azure DB, in case that is relevant. That said, how/where can I post the entire project? – E. A. Bagby Jan 03 '23 at 21:23
  • 1
    You can post on github or anything else – Simon Mourier Jan 04 '23 at 06:58
  • Added link to the entire project. – E. A. Bagby Jan 04 '23 at 15:20
  • I'm building and running (x86) file this project. Here is the list of files loaded, with versions, when I click on the button https://pastebin.com/raw/NmDKTYUy – Simon Mourier Jan 04 '23 at 16:55
  • Thanks, @SimonMourier. Though It builds fine for me too. It's the db scaffolding that creates the error. Or, and I missing something you've found? – E. A. Bagby Jan 04 '23 at 17:16
  • 1
    Ok I didn't understand your problem was still the scaffolding. This just doesn't work in x86 because when you start scaffolding from Visual Studio (2022 at least), it starts dotnet.exe in x64 (you can check that adding -verbose to the Scaffold-DbContext command) which cannot load the x86 hostpolicy (from .NET). If you really need to use x86 for scaffolding (you shouldn't) then you must use x86 dotnet and the ef.dll manually (for example in C:\Users\[yourlogin]\.nuget\packages\microsoft.entityframeworkcore.tools\6.0.12\tools\netcoreapp2.0\any – Simon Mourier Jan 04 '23 at 17:55
  • Thank you! A follow-up question: I created a new project, which defaulted to x86. Is there any reason, or does it just do what I did last time? I recall running into problems changing a project from x86 to x64 in vs2022 .NET 6. Any simple way to do this? Thanks again. – E. A. Bagby Jan 04 '23 at 17:59
  • 1
    It's mysterious (https://learn.microsoft.com/en-us/visualstudio/ide/how-to-configure-projects-to-target-platforms). some say it's alphabetical https://stackoverflow.com/questions/71654281/can-i-set-a-default-active-solution-platform-in-visual-studio – Simon Mourier Jan 04 '23 at 18:04
  • Progress! It worked. – E. A. Bagby Jan 04 '23 at 21:16

1 Answers1

0

Thanks to @Simon Mourier's comment, it turned out the solution and project needed to be set to x64. Note: I tried first setting them to ARM64, but that had the same issue.

Change the Solution platform by going to Build-->Configuration Manager.

Change the Project's Platform target by going Right-Clicking the projects-->Properties-->Build (General)-->Platform target

E. A. Bagby
  • 824
  • 9
  • 24