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

Feature like query tags but when calling SaveChanges #14078

Open
Tracked by #22959
xt0rted opened this issue Dec 4, 2018 · 3 comments
Open
Tracked by #22959

Feature like query tags but when calling SaveChanges #14078

xt0rted opened this issue Dec 4, 2018 · 3 comments

Comments

@xt0rted
Copy link

xt0rted commented Dec 4, 2018

I just setup a basic extension method that adds the calling file's path & line number as a tag to the given query:

public static class IQueryableTagExtensions
{
    public static IQueryable<T> TagWithFileInfo<T>(this IQueryable<T> source, [CallerFilePath] string fromFile = null, [CallerLineNumber] int onLine = 0, string comment = null)
    {
        var tagMessage = fromFile + "@" + onLine + (string.IsNullOrEmpty(comment) ? "" : " - " + comment);

        return source.TagWith(tagMessage);
    }
}

This works fine when querying for data, but I'd also like it to be part of the queries that execute when calling db.SaveChanges(). Is this possible currently? Adding query tags to updates is just as important, if not more important with this one, than adding them to selects.

The inspiration for this came from Stack Overflow who prepends this info to every query that runs through Dapper https://gist.github.com/NickCraver/c6f371bf8df37e05c4f09fd3c02ef6a2

What I really like about this is when using MiniProfiler this info becomes available making debugging easier. Then taken a step further it's included in Sentry's breadcrumbs that are part of logged exceptions, and it's also passed into App Insights. Both of those scenarios make production issues easier to track down.

Further technical details

EF Core version: 2.2.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win10
IDE: Visual Studio 2017 15.9.3

@divega divega changed the title Query tags when calling SaveChanges Feature like query tags but when calling SaveChanges Dec 7, 2018
@divega divega added this to the Backlog milestone Dec 7, 2018
@RKennedy9064
Copy link

Just wanted to check and see if there's been any progress on this? I have a similar situation where something like this would be really useful. I need to modify an insert statement for certain tables, but TagWith returns an IQueryable, so I don't see a good way of doing that. I'd rather not manually try to parse the CommandText to see if it's an insert statement and attempt to get the table name from it. Does something like this exist yet in efcore 5?

_context.DbSet.TagWith("-- Use hint: insert").AddRange(items);

public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
    DbCommand command, 
    CommandEventData eventData, 
    InterceptionResult<DbDataReader> result, 
    CancellationToken cancellationToken = default)
{
    if (command.CommandText.StartsWith("-- Use hint: insert", StringComparison.Ordinal))
    {
        // Do something with the insert command
    }

    return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
}

@ajcvickers
Copy link
Contributor

@RKennedy9064 You could try setting an AsyncLocal field on the context in SaveChanges, and then check it in the interceptor.

@aktxyz
Copy link

aktxyz commented May 10, 2022

One way to handle this is by adding some tags/fields to your datacontext, and then checking those tags/fields in the appropriate interceptor method ... see below. You can then use twiddle with your sql all you want. Just set these tags/fields on your datacontext before calling SaveChangesAsync.

    public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(
        DbCommand command,
        CommandEventData eventData,
        InterceptionResult<DbDataReader> result,
        CancellationToken cancellationToken = default)
    {
        var db = eventData.Context as MyDataContext;
        if (db != null && db.MyTag)
        {
            command.CommandText = <your sql twiddling here>;
            Console.WriteLine(1);
        }
        return new ValueTask<InterceptionResult<DbDataReader>>(result);
    }

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

7 participants