简体   繁体   English

EF核心可重用DbContext

[英]EF Core reusable DbContext

I'm trying to create a reusable base for future web applications made with asp net core. 我正在尝试为使用ASP Net Core制作的未来Web应用程序创建可重用的基础。 I created a library that contains a BaseDbContext that inherit from IdentityDbContext: 我创建了一个包含从IdentityDbContext继承的BaseDbContext的库:

public class BaseDbContext : IdentityDbContext<ApplicationUser>
    {
        public BaseDbContext(DbContextOptions options) : base(options)
        {

        }   
    }

Inside this library there are some services for login and creation of Users. 在该库中,有一些用于登录和创建用户的服务。

Everytime that I will be creating a new WebApplication I will reference the library and I will create a new DbContext like this: 每当我要创建一个新的WebApplication时,我都会引用该库,并创建一个新的DbContext,如下所示:

public class ProjectDbContext : BaseDbContext
{
    //some generics DBSET
    public ProjectDbContext (DbContextOptions<ProjectDbContext> options) : base(options)
    {
    }
}

And in the startup: 并在启动时:

    services.AddDbContext<ProjectDbContext>(options =>
    {
        options.UseSqlServer(connection);
    });

Since the service for the login and creation of users require a reference of BaseDbContext, I created a IDbContextFactory inside the base project that will be implemented by the main project like this: 由于用于登录和创建用户的服务需要引用BaseDbContext,因此我在基础项目中创建了一个IDbContextFactory ,该IDbContextFactory将由主项目实现,如下所示:

public class ProjectContextFactory : IDbContextFactory
{
    private readonly ProjectDbContext _projectDbContext;

    public ProjectDbContextFactory(ProjectDbContext remDbContext)
    {
        _remDbContext = remDbContext;
    }

    public BaseDbContext GetBaseDbContext()
    {
        return _projectDbContext;
    }
}

This factory will be used inside the base project to get a reference to the BaseDbContext. 该工厂将在基础项目中使用,以获取对BaseDbContext的引用。

Is this a good thing to do? 这是一件好事吗? Can this create some kind of problems? 这会带来一些问题吗?

In general, no, this is not a good thing to do. 通常,不,这不是一件好事。

that will contains the entities that will be used for all web applications 将包含将用于所有Web应用程序的实体

If there's entities that are common to all projects, then those should be factored out completely. 如果存在所有项目共有的实体,则应将它们完全排除。 In other words, you'd have one project (your base project) with a context like UserContext , which will have your User and Credential entities, and then every other project would have its own separate context that deals with just what it needs. 换句话说,您将拥有一个项目(您的基础项目),该项目具有诸如UserContext类的上下文,该上下文将具有您的UserCredential实体,然后每个其他项目都将拥有自己的单独上下文,该上下文可以处理所需的内容。 If the other application(s) need to access users, they'd either do so via an instance of UserContext or, better, through a service, such as an API. 如果其他应用程序需要访问用户,则可以通过UserContext实例,或者最好通过服务(例如API)来访问用户。

That said, it sounds like you're rolling your own auth, which you should emphatically not do. 这就是说,它听起来像你滚你自己的权威性,你应该强调这样做。 Use Identity. 使用身份。 And, if you need to share that between applications, you need a centralized auth provider, such as Identity Server. 而且,如果您需要在应用程序之间共享它,则需要一个集中的身份验证提供程序,例如Identity Server。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM