Skip to content

Include filtered entity on non filtered entity #142

@timg83

Description

@timg83

Hi

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

  • EntityFramework.DynamicFilters 3.0.1
  • Entity Framework 6.2.0 on SQL Server 2016

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.

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions