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

Filters not applied when using dbcontext's generic set method #47

Open
catalintoma opened this issue Sep 15, 2016 · 10 comments
Open

Filters not applied when using dbcontext's generic set method #47

catalintoma opened this issue Sep 15, 2016 · 10 comments
Assignees

Comments

@catalintoma
Copy link

catalintoma commented Sep 15, 2016

For example, in this case

dbContext.Filter<Company>(q => q.Where(c => c.Name == "abc"));

dbContext.Set<Company>().ToList();

The filter will not be applied.

There are already consumers of a context that use the generic method (UserManager for example)

In my case the fix was to override the set method and just forward the call to the property using reflection.
Basically, transformed from dbContext.Set to dbContext.Companies;

@zzzprojects zzzprojects self-assigned this Sep 16, 2016
@zzzprojects
Copy link
Collaborator

Hello @catalintoma ,

Which version of Entity Framework are you using?

For EF6, since version 1.4.x, EF+ Query Filter should support everything including set.

For EF5, this will be probably never be supported.

For EF Core, this will be eventually supported when EF Core will support Interceptor like EF6 do.

@catalintoma
Copy link
Author

catalintoma commented Sep 16, 2016

Hello,
Sorry for not mentioning, I am using the latest EF core.

Can you give me some info about what is different between calling the property and the set method?
I assume that EF will create a new query.

@zzzprojects
Copy link
Collaborator

For EF6,

Interceptor is used, so it allows us to support everything like Include method, Lazy loading, Set, etc.

For EF5 && EF Core,

The expression is directly modified at DbSet level. What that means is only LINQ using directly the context and the set are filtered.

Every time you create a new context and enable filters, we change all expression from all DbSet in the context for the filtered expression.

However, the Set method create a new DbSet

ServiceProviderServiceExtensions.GetRequiredService<IDbSetInitializer>(this.InternalServiceProvider))).CreateSet<TEntity>(this)

I will wait till EF team release the version 1.1 before checking this issue since they may do some Lifecycle hooks change which may allow to support this feature like we do for EF6: Lifecycle hooks

@deliotomaz
Copy link

deliotomaz commented Nov 27, 2016

Hi,
I am using a Generic Repository but Global Filters are not working.
In my repository I have a property:
protected DbSet<TEntity> DbSet { get { return Context.Set<TEntity>(); } }
I am setting the Global Filter on Context constructor:
QueryFilterManager.Filter<IEntidadeConta>("IEntidadeConta", q => q.Where(x => x.CodigoConta == dadosUsuarioResolver.CodigoContaCorrente)); QueryFilterManager.Filter<IExclusaoLogica>("IExclusaoLogica", q => q.Where(x => x.Ativo)); QueryFilterManager.InitilizeGlobalFilter(this);

The filter is not applied.
An important thing: I didn't create DBSet properties on my DbContext. If I create them, the filters are applied.

Can you help me ?

@zzzprojects
Copy link
Collaborator

Hello @deliotomaz ,

Try to create your own issue, it makes easier to answer & follow request

I have answered your question here: #87

@zzzprojects zzzprojects removed their assignment Jan 2, 2017
@JonathanMagnan JonathanMagnan self-assigned this Jan 3, 2017
@cindoum
Copy link

cindoum commented Jan 4, 2017

Any update here?

I am also using generic context.Set()... and cannot make it work.

@JonathanMagnan
Copy link
Member

Hello @cindoum ,

Are you using EF Core? If yes, there is nothing I can do automatically since Interceptor is not yet supported.

However, I could maybe add an extension method "SetFiltered" or something similar as a temporary fix.

context.SetFiltered<Customer>.Where(....);

Do you believe it's a good idea or you have a better one?

Best Regards,

Jonathan

@cindoum
Copy link

cindoum commented Jan 4, 2017

Thanks for your answer.

Yes i am using Ef core!

For now, there is no way of having filter for multi tenancy nor soft delete using ef core? Thats kind of strange.

If you have an idea on how this can be implemented using context.SetFiltered instead of context.Set that would be working, then i would love to see it. For me, it does not seem to be a problem as a workaround while the interceptor is implemented...

@JonathanMagnan
Copy link
Member

Hello @cindoum ,

The v1.4.21 is available.

The SetFiltered extensions method is now available but only work on type that is part of a DbSet or has been registered (See #87)

Best Regards,

Jonathan

@cindoum
Copy link

cindoum commented Jan 10, 2017 via email

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

4 participants