EntityFramework.DynamicFilters icon indicating copy to clipboard operation
EntityFramework.DynamicFilters copied to clipboard

Include filtered entity on non filtered entity

Open timg83 opened this issue 7 years ago • 6 comments

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.

timg83 avatar May 08 '18 12:05 timg83

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

JonathanMagnan avatar May 08 '18 23:05 JonathanMagnan

Please find attached the test project.

timg83_EFDynamicFilters.zip

timg83 avatar May 09 '18 06:05 timg83

Thank you,

The request will be assigned to one of my developers.

Best Regards,

Jonathan

JonathanMagnan avatar May 09 '18 10:05 JonathanMagnan

Hi.

Any news on this issue?

merken avatar Feb 27 '19 06:02 merken

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

JonathanMagnan avatar Mar 01 '19 04:03 JonathanMagnan

Thanks for clarifying, @JonathanMagnan

merken avatar Mar 01 '19 11:03 merken