0

I am developing the middle and back end of a .Net application whose Web API is built using .Net Core. However, the Data Access Layer and data persistence layer, as well as the business logic, are built in a project which is in .Net 4.6/EF6 (due to issues with migration management in .Net Core).

The architecture has Units of Work which manage business logic and live in the DAL (in EF6). Theoretically the Web API layer needs no direct access to the database; it should be able to work through the UoWs (which could just as easily be mocked, etc.) However, the only way I can see right now to get the UoWs instantiated properly is through a dependency injection chain that ultimately requires the (.Net Core) Web API to have access to the (EF6) ApplicationDbContext.

This is a problem because there are different versions of Identity Framework for EF6 and Core, and the Web API project is not happy to deal with my ApplicationUser object, which extends IdentityUser. Specifically, VS complains that The type 'IdentityUser' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.AspNet.Identity.EntityFramework, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35. So far as I can tell, that's not a dependency that can be satisfied in Core (which has its own implementation of Identity).

I am aware of the MS tutorial for how to deal with solutions that combine EF6 and Core projects, however this doesn't address the conflicting Identity dependencies. I'd love to have the DAL project handle its own dependency injection, but per this answer, I understand that a class-library-type situation has no container that would allow DI to happen.

In EF6 project I define ApplicationUser as:

using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity;

public class AppUser : IdentityUser<int, AppUserLogin, AppUserRole, AppUserClaim>{}

Over in the Web API (Core) project, I am attempting to inject it via:

    public class Startup
    {
        private readonly IConfiguration _config;

        public Startup(IConfiguration configuration)
        {
            _config = configuration;
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddScoped<AppDbContext>(_ => new AppDbContext());
            services.AddScoped<AppUserRepository<BaseFluentQuery<AppUser>>>()
        }
    }

where the BaseFluentQuery is a base class that provides a lot of the data-wrangling interface for the BL layer. I'm specifically getting the missing-reference error from the reference to AppUser, although I would also get an error if I replaced AppDbContext with IAppDbContext (which directly references the IdentityDbContext class).

Ideally I would be able to have a working project backended in EF6 with Identity, and a Core Web API that is blissfully ignorant of all of this. I'd be happy with a solution that resolves the conflicting Identity dependencies or one that organizes the DI better--or hey, convince me I can move to the Core version of the ORM!--but I'm just not finding the correct model out there.

Tiercelet
  • 147
  • 8
  • The question is: why are you building service layer in .NET Core and business layer in .NET 4.6? I think you should choose one of those and things will become easy...coherent at least! – Lorenzo Isidori Apr 24 '19 at 07:36
  • @LorenzoIsidori Good question; it's largely a decision driven by other members of my team, but there is functionality in EF6 (e.g. Table-per-Class inheritance) that is not (yet?) supported in EF Core, but no desire to build out a web API in an older product. – Tiercelet Apr 24 '19 at 17:28
  • you are right, TPC is not supported yet in EF Core. But at least it's in roadmap and accordint to this article it should be released soon: https://github.com/aspnet/EntityFrameworkCore/issues/3170 . No more I can tell you, I understand your concern. – Lorenzo Isidori Apr 26 '19 at 07:25

0 Answers0