Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data store command interception #1629

Closed
Bartmax opened this issue Feb 15, 2015 · 10 comments
Closed

Data store command interception #1629

Bartmax opened this issue Feb 15, 2015 · 10 comments

Comments

@Bartmax
Copy link

Bartmax commented Feb 15, 2015

Is there any extensible point on where EF hits the db, something that can be overridable to log or do anything everytime a hit to the db.

im wondering of something like

override OnDatabaseHit(Operation op, Context context) {
     if (typeof(op) == Query | SaveChanges | ... {
         Console.WriteLine(context.Query)
     }
}

if not, take it as a suggestion for EF7 :)

@Bartmax Bartmax changed the title Extens Extensibility point on EF for db hits. Feb 15, 2015
@rowanmiller
Copy link
Contributor

This is more generally covered by #626 but we'll leave this one active to track the particular scenario of intercepting database commands. Something like IDbCommandInterceptor from EF6.x.

@rowanmiller rowanmiller changed the title Extensibility point on EF for db hits. Data store command interception Feb 23, 2015
@rowanmiller rowanmiller added this to the Backlog milestone Feb 23, 2015
@NikolajDL
Copy link

Is there a time frame for something like IDbCommandInterceptor to be added to EF7? Using EF6 with ASP.NET 5 is a pain compared to EF7, but I really need the IDbCommandInterceptor to implement seamless row level security in EF7.

@rowanmiller
Copy link
Contributor

@excolo it will be post 1.0.0 release. You may be able to achieve something similar now if you start mucking around with replacing/wrapping low level services... but this item is about building a more usable experience.

@MariovanZeist
Copy link

@rowanmiller Can you give a hint on which low level service to wrap? I tried overriding the Open function in SqlServerConnection but it seems like this class is used for accessing metadata and not data related to a DbContext

    public class TestSqlServerConnection : SqlServerConnection
    {
        public TestSqlServerConnection(IDbContextOptions options, ILogger<SqlServerConnection> logger) 
            : base(options, logger)
        {
        }

        public override void Open()
        {
            base.Open();
            //Set session context
            var cmd = DbConnection.CreateCommand();
            cmd.CommandText = "EXEC sp_set_session_context @key=N'CompanyId', @value=1";
            cmd.ExecuteNonQuery();
        }
    }

@MariovanZeist
Copy link

It might have to do with how I register the above class (I might have to do some more detective work to get it to work)
I register the above class in my ConfigureServices function (before the call to AddEntityFramework) with

services.TryAddScoped<ISqlServerConnection,TestSqlServerConnection>();

But when I run the following code from dbContext it will still return an instance of SqlServerConnection instead of TestSqlServerConnection

    public class TestDbContext : DbContext
    {
        public ISqlServerConnection TestConnection()
        {
            return this.GetService<ISqlServerConnection>();
        }
    }

@rowanmiller
Copy link
Contributor

@MariovanZeist use AddScoped instead of TryAddScoped

@MariovanZeist
Copy link

@rowanmiller
My issue was actually RTFM related. Instead of creating the DbContext using DI, I was creating it by hand (using Activator.CreateInstance()) which I obviously shouldn't do. That's why it didn't pick up my ISqlServerConnection implementation.

I think i fell into the trap referenced by #4668

@rowanmiller
Copy link
Contributor

@MariovanZeist 👍 - the changes from #4668 are being worked on at the moment

@wpostma
Copy link

wpostma commented Jun 6, 2016

I put a question on stack-overflow to seek alternative techniques for EF Core.

http://stackoverflow.com/questions/37660746/can-i-configure-an-interceptor-yet-in-entityframework-core

Instead of a heavyweight framework approach perhaps whatever replaces interceptors could be a very lightweight and simple pattern.

@bricelam bricelam modified the milestones: Backlog, 2.1.0 Jan 2, 2018
@ajcvickers ajcvickers removed this from the 2.1.0-preview1 milestone Apr 26, 2018
@tourili
Copy link

tourili commented Jun 29, 2018

Hi there
What about Intercepting connection level to support Row Level Security. I checked the latest release, but still not supported yet.
I'm working around this lack by connecting the legacy handler Database.GetDbConnection().StateChange to monitor the connection state in order to inject my session context:

this.Database.GetDbConnection().StateChange += (s, e) =>
{
    if (e.OriginalState != System.Data.ConnectionState.Open && e.CurrentState == System.Data.ConnectionState.Open)
    {
        DbCommand cmd = this.Database.GetDbConnection().CreateCommand();
        cmd.CommandText = "EXEC sp_set_session_context @key=N'ObjectId', @value=@UserId";
        ....
    }
};

on a derived DbContext constructor.

I would like to see a better approach using DI to pipe this injection?
Thanks

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants