Skip to content

Plugins per-context (meta-issue) #748

Closed
Closed

Description

@austindrenski

This is a meta-issue to track a number of challenges related to using plugins on a per-context basis.

Plugin registration overridden with multiple contexts (#654)

Per #654 (comment):

INpgsqlOptions (e.g. opts in your snippet) is a singleton service, so it's likely that this is caused by the first configuration delegate being replaced (instead of wrapped by) the second configuration delegate.

Can we scope plugin registration by type of DbContext to avoid interfering with context-specific configurations?

Can't mix BCL and NodaTime types (#747)

Per #568 (comment):

Long story short, when you set up the ADO NodaTime plugin, it "hijacks" NpgsqlDbType.Timestamp and routes it to the NodaTime type handler, instead of the built-in DateTime type handler. The NodaTime type hander doesn't know how to write DateTime, so you get the above exception. EF Core, at its level, always sets NpgsqlDbType on parameters to unambiguously determine which PostgreSQL type it wants to send.

This unfortunately means that you can't mix both regular DateTime and NodaTime types in the same application.

Can we improve the per-connection plugin handling, such that it might be possible to support BCL and NodaTime types, as long as they are not mixed on the same DbContext?

// Keep BCL types for some entities and contexts
class LegacyEntity
{
    public DateTime SomeTimestamp { get; set; }
}

class LegacyDbContext : DbContext
{
    public DbSet<LegacyEntity> LegacyEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => base.OnConfiguring(optionsBuilder);
}

// While using NodaTime types for other entities and contexts
class FutureEntity
{
    public Instant SomeTimestamp { get; set; }
}

class FutureDbContext : DbContext
{
    public DbSet<FutureEntity> FutureEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) 
        => optionsBuilder.UseNodaTime();
}

Related

#568
#648
#654
#747
#757

Activity

  1. added this to the 3.0.0 milestone on Dec 11, 2018
  2. roji commented on Dec 13, 2018

    @roji
    Member

    Can we scope plugin registration by type of DbContext to avoid interfering with context-specific configurations?

    This is already the way EF Core works. If you look at the DbContext initialization code, you'll see that the service provider (which holds all the services) is cached on the context options, which themselves reference ContextType. In other words, if you have two distinct context types, each has its own type mapper and every other singleton service. You can easily test this by having two contexts, getting a service from each one (ctx.GetService<ITypeMappingSource>()) and comparing them for identity.

    The problem in #654 and similar issues is at the ADO level - although at the EF Core level each context has its own, isolated type mapper, configured only with the relevant plugins, at the ADO level we set up plugins globally. That is, the EF Core plugin sets up the ADO plugins on NpgsqlConnection.GlobalTypeMapper, meaning that it changes mappings for all connections, not just those used by the DbContext.

    As @lust4life noted in #747, the problem can be solved by registering the ADO plugin on a specific connection's type mapper, rather than on the global type mapper - this is well-supported at the ADO level, but not yet implemented at the EF Core plugin level. The reason I was hesitant about this was performance - currently, if any mapping is changed on the connection-specific type mapper, then when it is returned to the pool the mapper is disposed and rebuilt from scratch - a pretty heavy operation. I've just opened #2263 to speed that up, as there seem to be many users suffering from the lack of isolation here.

  3. austindrenski commented on Dec 13, 2018

    @austindrenski
    ContributorAuthor

    Perhaps I've misunderstood, but in #654 I believe the problem is that configuring a second DbContext without a plugin (after the first context does register a plugin) caused the first context's plugin to be unregistered.

    The overriding of the configuration delegate sounded like an EF Core configuration issue, but are you saying that this is also related to the ADO.NET layer?

  4. roji commented on Dec 13, 2018

    @roji
    Member

    Sorry, I may have conflated #654 with #747 (and other similar issues). I'm not sure exactly what's going on in #654.

    My mental model of EF Core is that each DbContext type has its own service set, and so at the EF Core level nothing is supposed to leak from context to context... Have you been able to reproduce this issue at the pure EF Core level (i.e. not ADO)?

  5. ajcvickers commented on Dec 13, 2018

    @ajcvickers

    @roji It's not quite like that. Typically, an application will have one service provider even for multiple types of context. That is, using a different context type does not in of itself result in a new service provider.

    However, each context instance also creates a D.I. scope. This means each context instance (and hence also each context type) has its own set of scoped services. But will use the same singleton services. This is one of the reasons why singleton services are a more efficient.

    This is coupled with the model cache, which is, by default, keyed off the context type. So each context type will have a different model. This is exposed via the IModel scoped service.

    Now it's possibly that some configuration of a context instance of any type may result in a new service provider being created. For example, using a context with a different database provider configured, or changing sensitive data logging, will trigger a new service provider to be created. This is because these things need to change the behavior of singleton services. Ideally, a typical application won't do this more than a few times at most.

  6. austindrenski commented on Dec 13, 2018

    @austindrenski
    ContributorAuthor

    @roji wrote:

    Have you been able to reproduce this issue at the pure EF Core level (i.e. not ADO)?

    I saw something similar in the wild a few months back, but didn't have time to isolate it. I'll put together an isolated repro in the next few days.

    Here's the singleton I had in mind with respect to configuration delegates being overwritten:

    https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/blob/73a73445f3a481d23f3fe8d0aba46a267a5df0b6/src/EFCore.PG/Infrastructure/Internal/INpgsqlOptions.cs#L8-L11

  7. roji commented on Dec 13, 2018

    @roji
    Member

    @ajcvickers thanks for correcting me on this, seems like my mental model of this wasn't quite right... I'll try to investigate and understand more this weekend. @austindrenski any repro here would be great...

  8. ajcvickers commented on Dec 13, 2018

    @ajcvickers

    @roji The mental model isn't that simple, so no worries. You have a better understanding than most. :-)

  9. roji commented on Dec 31, 2018

    @roji
    Member

    #654 is now closed as it automatically fixed in 2.2 by moving to the new EF Core plugin model.

    #757 seems like a duplicate of #747 to me - so does it make sense to close both #757 and this issue (#748), and leave only #747 to track per-connection plugin use?

  10. removed this from the 3.0.0 milestone on Jan 3, 2019
  11. added theissue type on Jun 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions