如何集成.Net 4.6 IdentityFramework数据访问层和.Net Core Web API

时间:2019-04-23 23:00:49

标签: c# .net .net-core

我正在开发.Net应用程序的中端和后端,其Web API是使用.Net Core构建的。但是,数据访问层和数据持久层以及业务逻辑是在.Net 4.6 / EF6中的一个项目中构建的(由于.Net Core中存在迁移管理方面的问题)。

该体系结构具有工作单元,用于管理业务逻辑并位于DAL(在EF6中)中。从理论上讲,Web API层不需要直接访问数据库。它应该能够通过UoW工作(可以很容易地被嘲笑,等等。)但是,我现在看到的正确实例化UoW的唯一方法是通过依赖注入链,该依赖注入链最终需要(.Net核心)Web API可以访问(EF6)ApplicationDbContext

这是一个问题,因为存在不同版本的Identity Framework for EF6和Core,并且Web API项目不满意处理扩展了ApplicationUser的{​​{1}}对象。具体地说,VS抱怨IdentityUser。据我所知,Core(具有自己的Identity实现)不能满足这种依赖关系。

我知道MS tutorial for how to deal with solutions that combine EF6 and Core projects,但是这不能解决冲突的Identity依赖项。我很想让DAL项目处理自己的依赖项注入,但是对于this answer,我知道类库类型的情况没有容器可以使DI发生。

在EF6项目中,我将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定义为:

ApplicationUser

在Web API(核心)项目中,我试图通过以下方式注入它:

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

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

其中 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>>>() } } 是基类,它为BL层提供了许多数据处理接口。虽然从BaseFluentQuery的引用中获取了丢失的引用错误,但是如果将AppUser替换为AppDbContext(直接引用{{1} }类)。

理想情况下,我将能够在EF6中使用Identity后端支持一个可运行的项目,并拥有一个对所有这些都一无所知的Core Web API。我对解决冲突的Identity依赖关系或更好地组织DI的解决方案感到满意-或,嘿,说服我可以使用ORM的Core版本!-但我只是找不到正确的解决方案在那里建模。

0 个答案:

没有答案