zzzprojects/EntityFramework.DynamicFilters

Issue with Filter Options

s21-himesh opened this issue · 2 comments

Hi,

I'm trying to use filter options with an interface. I have created an Interface and am expecting the filter to be applied to all entities that implement it. Unfortunately, I'm getting an error. I have updated to latest nuget (also updated ef nuget). Could you please help? I've changed to other interfaces I have implemented on this entity but still the same issue.

Filter used:-
modelBuilder.Filter(DefinedFilter.Context_Retailers.ToString(), (IRetailerFilter d, List valueList) => valueList.Contains(d.RetailerPrimaryKey), (BritanniaOnlineEntities dbContext) => FilterDelegates.GetContextRetailers(DefinedFilter.Context_Retailers, dbContext));

FilterDelegates.GetContextRetailers just returns a list of ID's

Interface:-

public interface IRetailerFilter
{
int RetailerPrimaryKey { get; }
}

Implemented on Entity:-

[Table("Retailer")]
public partial class Retailer : EFEntityBase, IHierarchyObject, IOrganisation, IRetailerFilter
{

[NotMapped]
public int RetailerPrimaryKey
{
get
{
return this.PrimaryKeyValue;
}
}

}

Inner Exception:-
{"Property RetailerPrimaryKey not found in Entity Type IRetailerFilter"}

The exception seems to be caused when the entity set is enumerated.

Stack Trace:-
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.CreateCommandDefinition(ObjectContext context, DbQueryCommandTree tree)
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlanFactory.Prepare(ObjectContext context, DbQueryCommandTree tree, Type elementType, MergeOption mergeOption, Boolean streaming, Span span, IEnumerable1 compiledQueryParameters, AliasGenerator aliasGenerator) at System.Data.Entity.Core.Objects.EntitySqlQueryState.GetExecutionPlan(Nullable1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__6() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
at System.Data.Entity.Core.Objects.ObjectQuery1.<>c__DisplayClass7.<GetResults>b__5() at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func1 operation)
at System.Data.Entity.Core.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption)
at System.Data.Entity.Core.Objects.ObjectQuery1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() at System.Data.Entity.Internal.LazyEnumerator1.MoveNext()
at System.Collections.Generic.List1..ctor(IEnumerable1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source) at BritanniaOnline.Data.Migrations.Configuration.Seed(BritanniaOnlineEntities context) in C:\src\Britannia Data Filtering\Britannia\BritanniaOnline\Data\Migrations\Configuration.cs:line 131 at System.Data.Entity.Migrations.DbMigrationsConfiguration1.OnSeed(DbContext context)
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClasse.b__d()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)

This library translates a linq expression into a database query - it doe not evaluate (or enumerate) expressions. The field you are referencing is "NotMapped" so it's not available to a database query.

More specifically, all referenced properties in the linq expression must be available in EF CSpace (which is a C# representation of the database model that EF creates from your C# classes). Since it's NotMapped, it's not known in CSpace.

Thanks. Problem solved and working great!