zzzprojects / EntityFramework.DynamicFilters

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

Include filtered entity on non filtered entity #142

Open timg83 opened 6 years ago

timg83 commented 6 years ago

Hi

Thanks for this awesome framework! Unfortunately we have a problem when querying an unfiltered entity and eager loading (include) a filtered entity.

Model

    abstract class BaseEntity
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int Id { get; set; }
    }

    interface ITenantAware
    {
        int Tenant_Id { get; set; }
    }

    abstract class TenantAwareEntity : BaseEntity, ITenantAware
    {
        public int Tenant_Id { get; set; }
    }

    class Accounting : TenantAwareEntity
    {
        public int Year { get; set; }
        public bool IsOpen { get; set; }
        public virtual ICollection<Period> Periods { get; set; }
    }

    class Period : BaseEntity
    {
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
        public virtual Accounting Accounting { get; set; }
        public bool IsOpen { get; set; }
    }

Filter OnModelCreating:

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

            modelBuilder.Entity<Accounting>();
            modelBuilder.Entity<Period>().HasRequired(p => p.Accounting).WithMany(b => b.Periods);

            base.OnModelCreating(modelBuilder);

            modelBuilder.Filter("TenantFilter", (ITenantAware e, int id) => e.Tenant_Id == id, () => Program.TenantId);
        }

Program.TenantId is in reality the tenantId of our principal object so this changes dynamically.

Executed query:

model.Period.Include(p => p.Accounting).Where(p => p.IsOpen).ToList();

Exception:

System.ApplicationException: 'FK Constriant not found for association 'EFDynamicFilters.Model.Period_Accounting' - must directly specify foreign keys on model to be able to apply this filter'

When executing the query starting from the tenantaware side => model.Accounting.Include(a => a.Periods).Where(a => a.IsOpen); there is no problem.

JonathanMagnan commented 6 years ago

Hello @timg83 ,

Do you think you could provide us a test project with this issue?

It will make easier/faster for my developer to getting started for investigating it.

We now always ask for a project since we found out that most issues are missing some essential information or are resolved by the requestor when creating it

(Even if the issue seem very easy to reproduce, by getting a test project, it allow us to give a faster support and better experience for the support of all our free libraries)

Best Regards,

Jonathan


Performance Libraries context.BulkInsert(list, options => options.BatchSize = 1000); Entity Framework ExtensionsBulk OperationsDapper PlusLinqToSql Plus

Runtime Evaluation Eval.Execute("x + y", new {x = 1, y = 2}); // return 3 C# Eval FunctionSQL Eval Function

timg83 commented 6 years ago

Please find attached the test project.

timg83_EFDynamicFilters.zip

JonathanMagnan commented 6 years ago

Thank you,

The request will be assigned to one of my developers.

Best Regards,

Jonathan

merken commented 5 years ago

Hi.

Any news on this issue?

JonathanMagnan commented 5 years ago

Hello @merken ,

The easy solution is to directly specify the foreign key

class Period : BaseEntity
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public virtual Accounting Accounting { get; set; }

    [ForeignKey("Accounting")]
    public int Account_Id { get; set; }
    public bool IsOpen { get; set; }
}

We didn't success to make it automatically without adding the foreign key yet ;(

Best Regards,

Jonathan

merken commented 5 years ago

Thanks for clarifying, @JonathanMagnan