EntityFramework.DynamicFilters
EntityFramework.DynamicFilters copied to clipboard
Include filtered entity on non filtered entity
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.
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 Extensions • Bulk Operations • Dapper Plus • LinqToSql Plus
Runtime Evaluation
Eval.Execute("x + y", new {x = 1, y = 2}); // return 3
C# Eval Function • SQL Eval Function
Thank you,
The request will be assigned to one of my developers.
Best Regards,
Jonathan
Hi.
Any news on this issue?
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
Thanks for clarifying, @JonathanMagnan