zzzprojects / EntityFramework-Classic

Entity Framework Classic is a supported version of the latest EF6 codebase. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
https://entityframework-classic.net
Other
103 stars 27 forks source link

ToList() returns no element #29

Closed yoazerty closed 5 years ago

yoazerty commented 5 years ago

I'm implementing a Multi-Tenant application with Z.EntityFrameWork on .Net Framework. It looks to work great with Z.EntityFrameWork.Plus.EF6 and methods DbSetFilter and even QueryFilterManager.Filter but as it’s a new applications I tried to move to the Classic Community version (7.0.38) and it is not working.

My global filter looks like this :

this.Configuration.QueryFilter.Filter<Employee>(x => x.Tenants.Where(z => 
    z.DateFrom <= DateTime.Now 
    && (z.DateTo == null || z.DateTo >= DateTime.Now) 
    && z.ID_Tenant == userTenantId).Count() > 0);

I'm trying to do some global filtering and the .ToList() or .ToArray() methods returns no row. But the .Count() returns the right number of rows.

context.Employees.ToList()  not working, empty list is returned context.Employees.Count()  Works!

Is this normal, could you help us on this point or do we have to forget this new Framework for the moment and keep the Plus EF6 version instead?

With the Sql Profiler I catched the query which returns the right rows. This makes me suppose that it's a bug in the Classic Community Framework.

JonathanMagnan commented 5 years ago

Hello @yoazerty ,

The QueryFilter in EF Classic doesn't let you filter navigation properties. That's why the ToList() return null. The count still works since it doesn't return entities but an int.

We will look at it later today as we can easily add an option to make it works for a basic situation like this one. However, making it work will turn off the compatibility for Lazy Loading with filter and maybe inheritance such as TPC, TPH, and TPT.

It's something that will also limit you?

We also can bring back an extension for EF6 very easily as we did to IncludeFilter and IncludeOptimized.

Best Regards,

Jonathan

JonathanMagnan commented 5 years ago

Hello @yoazerty ,

We looked at it and unfortunately, disabling QueryResultFilter have too much side impact.

For example, included entities are no longer filtered.

I believe this feature will be improved over time but at this moment, we cannot support it (I still remember it tooks several hundreds of hours in EF Plus!)

What we can do is releasing a new NuGet package Z.EntityFramework.Plus.QueryFilter.EFClassic that will use the code of EF Plus.

Let me know if that solution could work for you before we go ahead with it.

Best Regards,

Jonathan

yoazerty commented 5 years ago

Ok, thank you for your tests and quick support!

Don't worry, I'm going to go back to EF Plus, this is not a problem but what's the road map? Do you expect to deprecate or completely stop EF Plus? or can we still rely on it? I expect to use it in a production environment as soon as possible but the application is critical and I have to be sure that I can use it in a safe way (trust DbSetFilter).

Best regards, yoazerty.

JonathanMagnan commented 5 years ago

EF Plus

EF Plus will continue to live since it supports EF5, EF6, and EF Core.

However, we hope that library become fully integrated into EF Classic. So at one point, EF Plus will be 100% disconnected of EF Classic.

There is no need of a third party libraries if the features is directly integrated in the libraries itself.

EF Filter vs EF DbSetFilter

There is a huge difference between both, just to make sure even if you named it, which one are you using?

If you are using Query DbSetFilter, we can reproduce easily a similar behavior in EF Classic (that's what we tried yesterday and it worked from what we tested).

yoazerty commented 5 years ago

Hello,

Yes I'm using Query DbSetFilter. At first I was trying to use EF Filter but had some problems with the cache. So I switched to DbSetFilter which does exactly what I need.

For the moment I will continue to use it because I'm in a hurry but for sure, if one day we have to move to the Classic Framework, we would need to have this kind of behavior implemented in. Do you think it will be done?

Regards, yoazerty

JonathanMagnan commented 5 years ago

Hello @yoazerty ,

We don't feel comfortable to release a kind of temporary fix directly inside EF Classic.

However, we don't mind to release that feature under EF Plus for EF Classic.

A new package is now available: https://www.nuget.org/packages/Z.EntityFramework.Plus.QueryDbSetFilter.EFClassic/

I'm not sure what will be done this year, but we will for sure make add A LOT of stuff inside EF Classic through the year.

Let me know if we can close this issue.

yoazerty commented 5 years ago

Sorry for the delay,

Ok I can understand that you don't feel comfortable to release this in EF Classic for the moment, no problem. As I said, we are going to continue using the feature DbSetFilter of the Z.EntityFramework.Plus.EF6 V1.8.21 as it is working very well.

If one day we do not have the choice to move to the classic version, I hope you will have integrated this in, or be able to suggest a new solution for doing the same.

Personally, I prefer to use this one, based on the Microsoft EntityFramework, so it's ok. I suppose that with the Classic version we would have some other not expectected behaviours for the moment.

So you can close the issue, thank you for you support.