Full .NET references .NET Standard class library: FileNotFoundException

.net c# entity-framework-core nuget visual-studio

Question

I've just created 2 projects with Visual Studio 2019 (16.3.3):

  1. class library (netstandard2.0)
    • +Microsoft.EntityFrameworkCore, Version 2.2.4
    • +custom Repository class to hide DbContext class
  2. WPF project (v4.7.1)
    • +reference to the class library above
    • +some code to use the Repository:
var r = new Repository(@"Server=(localdb)\MsSqlLocalDB; Database=EfCore2Wpf; Trusted_Connection=True;");
DataContext = r.GetItems(); // FileNotFoundException here.

Here's the complete exception:

System.IO.FileNotFoundException: 'Could not load file or assembly "Microsoft.EntityFrameworkCore, Version=2.2.4.0, Culture=neutral, PublicKeyToken=adb9793829ddae60" or one of its dependencies. The specified module could not be found.'

That's right! There's no Microsoft.EntityFrameworkCore.dll inside \WpfApp\bin\Debug folder. But why?

(I have some legacy solutions with the same project types and they work. What's wrong here?)


For your information:

1) Including a single .NET Core package like Microsoft.EntityFrameworkCore.SqlServer cause a Million <Reference Include="..."><HintPath>..\packages\...dll</HintPath></Reference> entries. That's no option.

2) As far as I remember I had to migrate my legacy WPF projects to pass indirect .NET Core references: https://docs.microsoft.com/en-us/nuget/consume-packages/migrate-packages-config-to-package-reference. But currently I don't need any NuGet package in my WPF project so there's no package.config. And without a package.config file I cannot migrate!

1
0
11/5/2019 8:12:20 PM

Accepted Answer

It's a bug and not fixed because nobody cares: https://developercommunity.visualstudio.com/content/problem/769172/a-full-net-project-is-not-in-packagereference-form.html

Workaround:

  1. Add an arbitrary NuGet Package (e.g. NLog, AutoMapper, MvvmLightLibs or whatever).
  2. Right-click on the new packages.config file → "Migrate packages.config to PackageReference..." (you might not need this step if "PackageReference" is your "Default package management format", check Tools → NuGet Package Manager) enter image description here
  3. Works!

Or even faster: Add to your *.csproj

  <ItemGroup>
    <PackageReference Include="NLog" Version="4.6.7" />
  </ItemGroup>

Warning: Use tool support like ReSharper's "Optimize References..." with caution! It would remove the unused reference. :-(

0
11/15/2019 9:32:51 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow