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

DiagnosticListener.AllListeners #588

Open
amccorma opened this issue Jun 7, 2024 · 5 comments
Open

DiagnosticListener.AllListeners #588

amccorma opened this issue Jun 7, 2024 · 5 comments
Assignees

Comments

@amccorma
Copy link

amccorma commented Jun 7, 2024

Description

We use multiple database with different schemas with EF Core 7.0
We have a global command listener that changes the schema name before it executes.

Program.cs
DiagnosticListener.AllListeners.Subscribe(new GlobalListener());

public class GlobalListener : IObserver

 public void OnNext(KeyValuePair<string, object> value)
 {
     // there no key that matches this value.Key
     if (value.Key == RelationalEventId.CommandExecuting.Name)
     {
         // NEVER ENTERS THIS BLOCK
         var command = ((CommandEventData)value.Value).Command;
         command.CommandText = command.CommandText
             .Replace("[X.Shared].", "[X].[Shared].")
             .Replace("[X1.Common].", "[X1].[Common].")
             .Replace("[X2.Primary]","[X2].[Primary]")
     }
 }

The EntityFramework-Extensions never executes this block of code and I get exceptions saying the tables do not exists. It is the only way we can do it with our database design.

Is there a workaround for this issue? I need the schema/table names changed before the statements execute.

Exception

table does not exist

Exception message:
Stack trace:


- EF version: [EF Core v7.0.13]
- EF Extensions version: [EFE Core v7.100.0.0]
- Database Server version: [SQL Server 2022]
- Database Provider version (NuGet): [Microsoft.Data.SqlClient v5.1.2]
@JonathanMagnan JonathanMagnan self-assigned this Jun 7, 2024
@JonathanMagnan
Copy link
Member

Hello @amccorma ,

You are right; our library doesn't use a listener or interceptor.

You should be able to change the table name within our library with the PostConfiguration event.

Here is an example:

// globally
Z.EntityFramework.Extensions.EntityFrameworkManager.BulkOperationBuilder = builder => builder.PostConfiguration = configuration =>
{
	configuration.DestinationTableName = configuration.DestinationTableName
             .Replace("[X.Shared].", "[X].[Shared].")
             .Replace("[X1.Common].", "[X1].[Common].")
             .Replace("[X2.Primary]","[X2].[Primary]")
};

// or by operation (which override the globally for the specific option)
context.BulkSaveChanges(options => options.PostConfiguration = configuration =>
{
	configuration.DestinationTableName = configuration.DestinationTableName
             .Replace("[X.Shared].", "[X].[Shared].")
             .Replace("[X1.Common].", "[X1].[Common].")
             .Replace("[X2.Primary]","[X2].[Primary]")
});

It might be slightly different depending of what exactly you want to do, but I believe you will be able to find out how to make it work correctly with this information.

Let me know if that solves your current problem or if you need more help.

Best Regards,

Jon

@amccorma
Copy link
Author

amccorma commented Jun 7, 2024

That worked ok for the Framework.Extensions.

Do you have a similar method for the Z.EntityFramework.Plus framework = the future, cache functions. I checked and can not find anything. the code above does nothing for the Plus framework. Thanks

@JonathanMagnan
Copy link
Member

Hello @amccorma ,

Unfortunately, no, EF Plus (for EF Core) still doesn't support interceptors.

Best Regards,

Jon

@amccorma
Copy link
Author

amccorma commented Jun 10, 2024

i got it it working with adding an interceptor. I test it and it works. Ef Core 7.0

In EfContext class:

DbInterception.Add(new Org.Infrastructure.Interceptor.MyCommandInterceptor());

public class MyCommandInterceptor : Z.EntityFramework.Extensions.IDbInterceptor
{
    public void OnCompleted()
    {       
    }

    public void OnError(Exception error)
    {
        
    }

    public void OnNext(DiagnosticListener value)
    {
        value.Subscribe(new My2());
    }
}
public class My2 : System.IObserver<KeyValuePair<string, object?>>
{
    public void OnCompleted()
    {
    }

    public void OnError(Exception error)
    {
    }

    public void OnNext(KeyValuePair<string, object?> value)
    {
        if (value.Key == "Microsoft.Data.SqlClient.WriteCommandBefore" ||
            value.Key == "Microsoft.EntityFrameworkCore.Database.Command.CommandInitialized")
        {
            var obj = ToDynamic(value.Value);
            var command = obj.Command as Microsoft.Data.SqlClient.SqlCommand;
            if (command != null)
            {
                command.CommandText = command.CommandText
                        .Replace("[X.X1]", "[X.[X1]")
                        .Replace("[X3.X4].", "[X3].[X4].");
            }
        }
    }

@JonathanMagnan
Copy link
Member

Indeed, I was wrong,

It makes sense that it works with the Query Cache features in EF Core.

I'm surprised that you were able to make it work for Query Future. Thank you for sharing your solution. I tried it, and it worked, so we will surely look more deeply into it.

Best Regards,

Jon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants