Could not load file or assembly class library "Microsoft.EntityFrameworkCore version = 6.0 in Class Library

Kmcnet 691 Reputation points
2022-06-28T00:25:56.403+00:00

Hello everyone and thanks for the help in advance. I have two problems intelrinked with each other. First, I am creating for the first time a C# class library in .Net 6 that is a data access layer for other apps. I have never created a class library before, so I may not be doing so correctly, however, I have read the Microsoft documentation and think I did it correct. I then referenced the library by adding the dll file by "Add Project Reference". When I run the application, I receive the error: Could not load file or assembly class library "Microsoft.EntityFrameworkCore version = 6.0 in Class Library. To clarify, the class library uses EntityFramework Core 6.06. I have tried deleting the bin and obj folders ad rebuilding, but it doesn't fix the problem. I am not sure what the next step is to solve this.

Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
697 questions
.NET CLI
.NET CLI
A cross-platform toolchain for developing, building, running, and publishing .NET applications.
322 questions
{count} votes

Accepted answer
  1. AgaveJoe 26,191 Reputation points
    2022-06-28T14:26:55.313+00:00

    I then referenced the library by adding the dll file by "Add Project Reference".

    You created a specific file reference. The class library contains other library dependencies that the main application knows nothing about because the dependent DLLs are in the class library's bin folder.

    Assuming both projects are part of the same solution, create a project reference rather than referencing the DLL directly (file reference). Otherwise, you'll need to physically copy the dependent DLLs or make a reference to the missing libraries in the main project both of which can cause headaches down the road.

    Reference documentation.
    Manage references in a project

    0 comments No comments

3 additional answers

Sort by: Most helpful
  1. Kmcnet 691 Reputation points
    2022-06-28T11:55:24.68+00:00

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

    <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    </PropertyGroup>

    <ItemGroup>
    <PackageReference Include="LumenWorksCsvReader" Version="4.0.0" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.6" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="6.0.6" />
    </ItemGroup>

    </Project>


  2. Kmcnet 691 Reputation points
    2022-06-28T13:24:17.58+00:00

    Correct. Here is the project using the class:

    <Project Sdk="Microsoft.NET.Sdk">  
      
      <PropertyGroup>  
        <OutputType>WinExe</OutputType>  
        <TargetFramework>net6.0-windows</TargetFramework>  
        <Nullable>enable</Nullable>  
        <UseWindowsForms>true</UseWindowsForms>  
        <ImplicitUsings>enable</ImplicitUsings>  
      </PropertyGroup>  
      
      <ItemGroup>  
        <Folder Include="data\csv\" />  
        <Folder Include="data\pdf\" />  
      </ItemGroup>  
      
      <ItemGroup>  
        <PackageReference Include="LumenWorksCsvReader" Version="4.0.0" />  
      </ItemGroup>  
      
      <ItemGroup>  
        <Reference Include="SpotScreenerLibrary">  
          <HintPath>..\..\SpotScreenerLibrary\SpotScreenerLibrary\bin\Release\net6.0\SpotScreenerLibrary.dll</HintPath>  
        </Reference>  
      </ItemGroup>  
      
    </Project>  
    

    So here comes the stupid questions. I assumed since the packages are installed on the class library, it si not necessary to install each of the same packages on the consuming project. Am I misunderstanding this?


  3. Kmcnet 691 Reputation points
    2022-06-28T21:02:36.453+00:00

    Thanks for the response.

    The class library contains other library dependencies that the main application knows nothing about because the dependent DLLs are in the class library's bin folder.

    This is incorrect. None of the dependent dll files appear in either the debug or release directories within the bin folder. However, once the missing libraries were added the error resolved. So this now begs other questions. What is the best practice for creating a standalone class library (I created this library separately from the other project). It seems strange that the concept of a class library created to be consumed my different applications requires the library be developed as a part of a solution. I assume you would need to make this library a part of every application that calls it?

    0 comments No comments