samedi 12 mars 2022

Can't read types from assembly using EntityFramework (.net core 5.0)

I am trying to read the types out of an assembly that contains Entity framework, but am getting an error saying:

Could not load file or assembly '..file path..\TestRoslyn\Database\bin\Debug\net5.0\Database.dll'. The system cannot find the file specified.

The code I am using to read the types is pretty simple:

using System;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Build.Locator;
using Microsoft.CodeAnalysis.MSBuild;

namespace TestRoslyn
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (!MSBuildLocator.IsRegistered) MSBuildLocator.RegisterDefaults();
            using var w = MSBuildWorkspace.Create();
            // Substitute your file location
            var basePath = @"C:\Users\username\source\repos\";
            var slnFile = @$"{basePath}TestRoslyn\TestRoslyn.sln";
            var sln = await w.OpenSolutionAsync(slnFile);
            foreach (var p in sln.Projects)
            {
                var asm = Assembly.LoadFrom(p.OutputFilePath);
                foreach(var t in asm.GetTypes())
                    Console.WriteLine($"{p.OutputFilePath}\t{t.FullName}");
            }
        }
    }
}

This works as is. However, when I add a simple project to the solution that references nuget package Microsoft.EntityFrameworkCore (5.0) with one file:

using Microsoft.EntityFrameworkCore;

namespace Database
{
    public class AppContext: DbContext
    {
    }
}

(To keep it simple, I didn't include anything in the AppContext class except the base class.)

When I add a project with the EntityFramework nuget package to the solution above with just this class I get the error indicated above.

Not sure what the cause is? Do I need to somehow load the nuget package into the workspace? If so, how?





Aucun commentaire:

Enregistrer un commentaire