EntityFramework.DynamicFilters: Filter not working on TPT

Hi, I’m try to use your awesome EF filtering model. All features seems good in every case, but I still not get it working in a particular case. I’ve an abstract Person class, that implement the field for Tenant filtering and a Customer and Supplier classes, inherited from Person, that I want to be filtered (separately) by the current user Id. The field are in the table related to the abstract base class, but I’ve applied the filters on concrete Customer and Supplier models.

I’ve implemented filters on model builder like this (Cliente is Customer and Fornitore is Supplier):

``modelBuilder.Filter(“CustomersMultiTenancy”, (Cliente cli, int tenantId, bool isMultiSocieta) => (cli.SocietaProprietariaId == tenantId) || (cli.IsMultiSocieta == isMultiSocieta), () => KAnMoUser.Current.SocietaId, () => true); modelBuilder.Filter(“SuppliersMultiTenancy”, (Fornitore forn, int tenantId, bool isMultiSocieta) => (forn.SocietaProprietariaId == tenantId) || (forn.IsMultiSocieta == isMultiSocieta), () => KAnMoUser.Current.SocietaId, () => true); I’ve use SQL Profiler to check the generated SQL query but, in that cases, filter are not applied on the generated SQL.

Where I’m wrong? Same filter application method, but on class Category (not inerited from abstract base, all field in the same table) and the dynamic filter work properly. I’m wondering from this morning on this without finding a solution!

I hope in your help!

Daniel

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 28

Commits related to this issue

Most upvoted comments

Sorry it didn’t get done sooner, but (thanks to another user) I just pushed this fix into v2.7 of the NuGet package. Thank you very much for posting this!

Hi, after some other attempt, I found the source of the problem (not the solution yet!). your containers property (the db context) not contain an EntitySet for the derived entity types. So, there is an EntitySet of Anagrafiche, but not an EntitySet of Fornitori. I think this is due to the fact of inheritance strategy. I adopted TPT, so I’ve a Table for abstract Anagrafica entity type, and a Table for derived Fornitore (supplier) entity type.

The only manner how this works is to load navigation property by this way:

` var listini = Repository.Entities.ToList();
foreach (var l in listini) { Repository.Context.Entry(l).Reference(f => f.Fornitore).Load(); }

            return listini.Where(f => f.Fornitore != null && (f.Fornitore.IsMultiSocieta 
                || f.FornitoreId.Equals(KAnMoUser.Current.SocietaId))).ToList();`

But, in this way, I’m not causing an n+1 query loop?

May be I need to remove filter from derived type do the trick? This is not very comfortable because I want to enable my users to choose the behavior of shared resource (customers and suppliers into the software). By installation configuration parameter, the software could be allow to share customers between multiple company but not supplier, or viceversa, or booth or no one! In this manner, I can only decide to share booth customer and supplier all togheter.

And, finally, I’m also not sure that if I remove the filters from customers and suppliers (Cliente and Fornitore) and put them just on Anagrafica (base common type) this can resolve the query visitor issue. What you think about that?

Thanks in advance. Daniel

Hi jcachat, I’ve cloned your repository and debugged source code. Exception was thrown at the line

var entitySet = containers.EntitySets.FirstOrDefault(e => e.ElementType.Name == baseResult.ResultType.EdmType.Name);

of DynamicQueryVisitorCSpace.cs. The problem seems to be that EntitySets not contain the EntitySet of Fornitore. Fornitore is a derived type from Anagrafica. In my DbContext, I’ve correctly defined a DbSet<Fornitore> Fornitori {get; set;} but I can’t see it into containers.EntitySets collection.

Have you some idea about that?

Thanks in advance! Daniel