zzzprojects / EntityFramework.DynamicFilters

Global filtering for Entity Framework.
https://entityframework-dynamicfilters.net/
MIT License
501 stars 86 forks source link

Configure For Database/Model First #133

Closed romainecarter closed 6 years ago

romainecarter commented 6 years ago

Based on the documentation I see where the filters are configured through OnModelCreating override, Is there a way to do this using Database first?

JonathanMagnan commented 6 years ago

Unfortunately no,

You can use EF+ Query Filter but it doesn't work as good as this library for by example Tenant scenario.

Let me know if that successfully answer your question.

Best Regards,

Jonathan


Help us to support this library: Donate

romainecarter commented 6 years ago

Thanks I will look into EF + Query Filter.

romainecarter commented 6 years ago

Below example the filter is not added, is this one of the limitations you mentioned?

using System;
using System.Data.Entity;
using System.Linq;
using Z.EntityFramework.Plus;
namespace DbContextAOP
{
    class Program
    {
        static void Main(string[] args)
        {
            QueryFilterManager.Filter<Employee>(q => q.Where(x => x.BusinessEntityID == 250));

            using (var db = new AdventureWorks2016CTP3Entities())
            {              
                var purchase = db.PurchaseOrderHeader.Include(a=>a.Employee).Select(i => i.Freight + " " +  i.Employee.BusinessEntityID);

                foreach (var m in purchase)
                {
                    Console.WriteLine(m);
                }
            }
        }
    }
}

Generated SQL

SELECT 
     CAST( [Extent1].[Freight] AS nvarchar(max)) + N' ' +  CAST( [Extent1].[EmployeeID] AS nvarchar(max)) AS [C1]
    FROM [Purchasing].[PurchaseOrderHeader] AS [Extent1]
JonathanMagnan commented 6 years ago

Hello @romainecarter ,

I believe you are missing the QueryFilterManager.InitializeGlobalFilter(ctx); code.

Global filter is not applied by default. It's normally recommended to add it in your context constructor if you can.

Best Regards,

Jonathan


Help us to support this library: Donate

romainecarter commented 6 years ago

Thanks @JonathanMagnan , I added that but getting the same results.

romainecarter commented 6 years ago

Seems adding a navigation property to the query as in the above example does not work and the where clause is dropped in the generated SQL.

JonathanMagnan commented 6 years ago

Hmmm,

Looking at the source code, we only support filtering list but recently, we added a new property QueryFilterManager.AllowPropertyFilter = true; which also to support navigation property that is not list but item.

Could you try with this option?

Best Regards,

Jonathan


Help us to support this library: Donate

romainecarter commented 6 years ago

That worked!

JonathanMagnan commented 6 years ago

That's great ;)


Help us to support this library: Donate