Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could not load file or assembly Microsoft.EntityFrameworkCore #9251

Closed
humzakhan opened this issue Jul 24, 2017 · 11 comments
Closed

Could not load file or assembly Microsoft.EntityFrameworkCore #9251

humzakhan opened this issue Jul 24, 2017 · 11 comments
Labels
closed-no-further-action The issue is closed and no further action is planned.

Comments

@humzakhan
Copy link

Issue

I have a class library project targeting .NET Core 1.1 with Entity Framework Core v1.1.2 installed. It contains all the domain logic. Whenever I call methods from my EF Core project in other projects, I'm having this exception:

Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

All my projects are targeting .NET Core 1.1 and I'm not sure what is causing this behavior. Any ideas? All the dependencies are installed correctly, database migrations work perfectly fine but I'm not able to use any of my methods involving database calls. It fails with the exception as mentioned above.

@smitpatel
Copy link
Contributor

Make sure that all the packages of AspNet core you are referring are from 1.1.2 release. Mixing versions from different releases are not supported and may not work always. If you are unsure then share your csproj file.

If above doesn't help then you may need to generate binding redirects. See dotnet/msbuild#1310 for the issue and work-around.

@humzakhan
Copy link
Author

humzakhan commented Jul 24, 2017

@smitpatel

Here's my csproj file:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="1.1.2" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.0" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.0" />
  </ItemGroup>
  <ItemGroup>
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.Dotnet" Version="1.0.1" />
  </ItemGroup>
  <ItemGroup>
    <Reference Include="Raptor.Core">
      <HintPath>..\Raptor.Core\bin\Debug\netcoreapp1.1\Raptor.Core.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

All the dependencies are at their latest versions for .NET Core 1.1. I'm looking into binding redirects now but I'm not sure what they are and how they can help address my issue. Can you provide a bit more information regarding it?

@smitpatel
Copy link
Contributor

https://www.nuget.org/packages/Npgsql.EntityFrameworkCore.PostgreSQL/1.1.0

Package Npgsql.EntityFrameworkCore.PostgreSQL with version 1.1.0 depends on Microsoft.EntityFrameworkCore version 1.1.0 but you are referencing to 1.1.2 explicitly. That is causing the issue.

  1. Don't add reference to EFCore package directly. PostgreSQL package will bring it in transitively anyway.
  2. EFCore.Tools package will also bring EFCore.Design transitively.
  3. Given PostgreSQL is on 1.1.0 version, all EF package versions must be aligned to 1.1.0. The associated version of dotnet ef cli (Microsoft.EntityFrameworkCore.Tools.Dotnet package) is 1.0.0.

This is documentation for Binding redirect. https://docs.microsoft.com/en-us/dotnet/framework/configure-apps/file-schema/runtime/bindingredirect-element

Essentially, when you have multiple versions of same assembly referenced in your project, you need to generate a binding redirect so that calls to all different versions of assembly can be redirected to single version which is actually made available to project. In the absence of that, you program will try to load different version of assembly than what is available and throw above exception.

In above case, you are referencing different versions of EFCore package. Different versions of same package have different assembly versions in them.

@smitpatel smitpatel added the closed-no-further-action The issue is closed and no further action is planned. label Jul 25, 2017
@roji
Copy link
Member

roji commented Jul 25, 2017

@smitpatel, I was under the impression it's totally fine to have a lower version of a provider referencing a high version of the core, since patch versions supposed to maintain 100% backwards compatibility. Nuget is supposed to automatically generate binding redirects to the latest version of EF Core (maybe some issue with that is at the source of the problem here?). Unless I'm missing something, the csproj above in itself isn't supposed to create any issues...

@smitpatel
Copy link
Contributor

@roji - You are right in the terms of compatibility though binding redirects are not generated always for class library. See dotnet/msbuild#1310

@roji
Copy link
Member

roji commented Jul 25, 2017

OK, wasn't aware of the library binding redirect issue... In any case it seems odd to ask provider writers to release patch versions for every EF Core patch version, even if there's nothing new on their end, solely to update the dependency etc.

In any case FYI I plan to release Npgsql.EntityFrameworkCore.PostgreSQL 1.1.1 later today, which will depend on the latest EF Core, so in this particular instance the problem should go away.

@smitpatel
Copy link
Contributor

I suppose patches for EF are not critical (non-security kind so far). So it would be fine not to move to patch version unless you need a fix which is in patch.

even if there's nothing new on their end, solely to update the dependency etc.

That is interesting question because we release new versions even without fix because dependency changed. I am not certain if that was done because its just single release cycle or we want to update. Perhaps @divega or @ajcvickers can provide guidance on what provider writers should do.
If we decide that provider's package will not be updated due to dependency change then we may need to publicize limitation of binding redirect more visible to customers (or in optimistic mode, get the issue fixed in msbuild)

@roji
Copy link
Member

roji commented Jul 25, 2017

@smitpatel

That is interesting question because we release new versions even without fix because dependency changed. I am not certain if that was done because its just single release cycle or we want to update. Perhaps @divega or @ajcvickers can provide guidance on what provider writers should do.

Yeah, it seems that up to now there's been a general single release cycle (.NET Core/EF Core/Microsoft.Extensions), which is probably at least part of the story.

I just released Npgsql's provider version 1.1.1, which takes a dependency on EF Core 1.1.2 (simply because I didn't release sooner), I could have skipped 1.1.1 and released 1.1.2 to maintain version parity with you guys. However, imagine what happens if I have a critical fix I need to get out - unless I go with 1.1.1.1 we lose version sync (since I'm forced to publish 1.1.2 while EF Core is still at 1.1.1).

As a general statement, I'm not sure I see the enormous value in having version sync across the board. Now that .NET Core/Standard 2.0 is out and presumably things stabilize a little, it would make sense for me for the EF Core team to start having its own release schedule based on its own features/progress rather than get tied down by what's happening in .NET Core etc.

Anyway, just some thoughts, nothing very critical...

@humzakhan
Copy link
Author

humzakhan commented Jul 25, 2017

@smitpatel I uninstalled all the dependencies and installed only the high-level ones (as you mentioned, they bring the required ones along with them). This is my csproj file now:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="1.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="1.1.1" />
    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL.Design" Version="1.1.1" />
  </ItemGroup>
</Project>

But I'm still getting the same error, but I have noticed something.

Message: System.IO.FileNotFoundException : Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=1.1.2.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the file specified.

Focus on this line:

Could not load file or assembly 'Microsoft.EntityFrameworkCore, Version=1.1.2.0

Why is my class library project looking for the version 1.1.2? I uninstalled everything and installed the main dependencies only, which in turn installed EF Core and others. Not sure why the application is looking for v1.1.2 though, when clearly everything is at v1.1.

@roji
Copy link
Member

roji commented Jul 25, 2017

@humzakhan as I wrote above, the newly-released Npgsql.EntityFrameworkCore.PostgreSQL 1.1.1 depends on Microsoft.EntityFrameworkCore 1.1.2, the versions aren't in sync; you can see this in the dependencies section of the nuget page. This isn't a problem - Npgsql is supposed to bring in the proper version of EF Core and that's that.

The question is why your build isn't finding EF Core 1.1.2. I'm assuming you've properly restored after making the change, try to understand what's being brought in and what assemblies are alongside your binary if the error occurs while running (as opposed to building).

@humzakhan
Copy link
Author

Fixed it. The class library project which had all these EF packages installed (or my data access layer) was being referenced as an assembly in the main project. Instead of that, I referenced it as a project and everything worked fine. Probably this is some sort of issue with referencing class libraries present on disk, I read about it somewhere. Will open an issue regarding this on the .NET Core repository because that is where it belongs (if I'm not mistaken).

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned.
Projects
None yet
Development

No branches or pull requests

4 participants