Superslinky
Superslinky

Reputation: 53

Reference Entity Framework 6 in .NET Core through another project

I added a new ASP.NET Core Web Application (Web API) project in my solution.

I understand the fact that I can't use EF6 in this project but I would like to know if there is a way to make another project (using .NET Framework) do the work and pass the data back to the .NET Core project.

Here is my code :

. Controller (.NET Core)

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet]
    public UserDto Get(Int32 id)
    {
        Models.UserDto user = UserHelper.Get(id);

        if (user == null)
            throw new Exception();

        return user;
    }
}

. Helper (.Net Core)

public class UserHelper
{
    public static UserDto Get(int id)
    {
        User user = UserController.Get(id);
        return Map(user);
    }

    private static UserDto Map(User user)
    {
        if (user == null)
            return null;

        UserDto dto = new UserDto();
        dto.Id = user.ID;
        dto.Lastname = user.LastName;
        dto.Firstname = user.FirstName;
        dto.Email = user.Email;
        dto.Username = user.Username;
        dto.PhoneNumber = user.PhoneNumber;
        dto.CustomerId = user.CustomerID;
        return dto;
    }
}

. Controller (.NET Framework)

public static User Get(int id, UserStateCriteria criteria)
    {
        InputCheckerHelper.CheckNull<UserStateCriteria>(criteria, "criteria");

        using (NContext uow = new NContext())
        {
            var query = uow.Users
                           .Include(u => u.ConsoleUsers)
                           .Include(u => u.Devices)
                           .Include(u => u.Culture)
                           .Include(u => u.Product)
                           .Include(u => u.Customer)
                           .Include(u => u.ProductCredentials.Select(pc => pc.Database.DatabaseConnector))
                           .Include(u => u.ProductCredentials.Select(pc => pc.Database.DatabaseConnector.Methods.Select(m => m.Headers)))
                           .Include(u => u.ProductCredentials.Select(pc => pc.Database.DatabaseConnector.Methods.Select(m => m.Parsers)))
                           .Include(u => u.ProductCredentials.Select(pc => pc.Database.DatabaseConnector.Methods.Select(m => m.ConfigurableConnectorFieldRenamings)))
                           .Include(u => u.ProductCredentials.Select(pc => pc.Database.DatabaseConnector.Methods.Select(m => m.ConfigurableConnectorErrorMappings)))
                           .Include(u => u.Subscription.Edition)
                           .Where(u => u.ID == id);

            query = ApplyStateCriteriaOnQuery(criteria, query);

            return query.FirstOrDefault();
        }
    }

I have search SO and other web sites in vain for a case similar to mine but it was not very helpful.

I am only asking here because on microsoft's site it says :

The recommended way to use Entity Framework 6 in an ASP.NET Core application is to put the EF6 context and model classes in a class library project that targets the full framework.

But after referencing my BLL and DAL project I get :

System.IO.FileNotFoundException: 'Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.'

Upvotes: 0

Views: 1690

Answers (1)

Chris Pratt
Chris Pratt

Reputation: 239420

You can if an only if your Core app is actually running on the full framework, as well. The reason is simple, you can't reference a full framework library from a non-full framework project. The API footprint is not the same.

However, even if you're running on the full framework, you still must have a separate project (a class library, for example) to actually house your context and migrations. This is because the PMC commands you need to use with EF6 aren't compatible with a Core project.

Upvotes: 1

Related Questions