Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does not help with implementing basic OOP #2820

Closed
DimitryNechaev opened this issue Oct 30, 2020 · 2 comments
Closed

Does not help with implementing basic OOP #2820

DimitryNechaev opened this issue Oct 30, 2020 · 2 comments

Comments

@DimitryNechaev
Copy link

This article implies that if I have 10 entities which have the same field "IsDeleted" I need to copy-paste the filter 10 times.
Basic OOP expects to have a single base entity with the field and the rule applied on the base level.

At the moment I greatly struggle with EF Core trying to do exactly this and nowhere can find applicable article.
In my case a large number of 3rd pty tables having StatusId field, which I mage though basic abstract class HasStatus.

The best so far is that I manage to enumarate though all entities but can't apply a filter, as there is no generic to accept the base type. HasQueryFilter accepts LambdaExpression, which is a bummer.

        foreach (var entity in modelBuilder.Model.FindLeastDerivedEntityTypes(typeof(HasStatus)))
        {
            modelBuilder.Entity(entity.ClrType)
                .HasQueryFilter(t => t.StatusID == 1);
       }

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

@ajcvickers
Copy link
Member

@DimitryNechaev This is one of the usability issues around query filters that are being tracked on dotnet/efcore#21459. Closing this as a duplicate of that issue, with a reference to here.

@stevendarby
Copy link
Contributor

FWIW you can build the expression doing something like so:

foreach (var entity in modelBuilder.Model.FindLeastDerivedEntityTypes(typeof(HasStatus)))
{
    var statusIdProperty = entity.FindProperty(nameof(HasStatus.StatusID)).PropertyInfo;
  
    var parameterExpression = Expression.Parameter(entity.ClrType);
    var propertyExpression = Expression.Property(parameterExpression, statusIdProperty);
    var constExpression = Expression.Constant(1);

    var equalExpression = Expression.Equal(propertyExpression, constExpression));
    
    var filter = Expression.Lambda(equalExpression, parameterExpression);

    modelBuilder.Entity(entity.ClrType).HasQueryFilter(filter);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants