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

Query: Model-level filters for queries #5774

Closed
AdrianHostettler opened this issue Jun 15, 2016 · 12 comments
Closed

Query: Model-level filters for queries #5774

AdrianHostettler opened this issue Jun 15, 2016 · 12 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@AdrianHostettler
Copy link

Hy there, hope you're all doing well. ;)

This is a Feature Request.

I was wondering, if EF Core will support/implement a Filtering functionality for e.g. SoftDeletion configurable in a CustomDbContext.OnModelCreating(ModelBuilder builder) like this:

builder.Filter<IBaseEntity>(e => e.Where(x => !x.IsSoftDeleted));

Whereas IBaseEntity Interface ensures that the concerned models have the IsSoftDeleted property.

In addition it would also be nice, if this filter could be turned on and off, if e.g. i would need to access the deleted records for administration/historyview purposes.

There is a library from zzzprojects EntityFramework-Plus that would support this in older versions of EF, but it would be nice, if the desired functionality would be working out of the box/native, without using that library.

The creators of EntityFramework-Plus note on their page, that

Entity Framework 7:
DOES NOT support filter by inheritance/interface (Will be supported when EntityFramework team will fix this issue: #3736)

So i'd like to know, if we can expect this feature to be built in on 1.0.0 (Tools Preview 2) Milestone at 27 June 2016, or if this bug will be looked at, so that zzzprojects could bring out a new version of their libraries.

Please let me know what your plans are concerning that topic, I hope I'm not the only one, that would love that feature, otherwise i would probably have to do that filtering manually in all my Repositories/DAL.

Kind regards

Adrian

@rowanmiller rowanmiller changed the title DbContext - ModelBuilder - Filter Base Query Filter (Defining Query) Jun 15, 2016
@rowanmiller
Copy link
Contributor

Agreed the idea of a global filter would be useful. Popping on the backlog as it is lower priority than some things we are going to tackle first. BTW we have an issue specifically for soft delete (#4025), but we also want to track this more general idea.

Regarding EntityFramework-Plus, we have #3736 on our candidate list to address in the 1.1.0 release.

@rowanmiller rowanmiller added this to the Backlog milestone Jun 15, 2016
@julielerman
Copy link

oh what a coincidence that you commented on this just a few minutes ago! I"m trying to use an interface in onmodelcreating and have been looking for workarounds to the requirement that modelbuilder.Entity requies a reference type and won't accept an interface. Any temporary workarounds that you know of? Thanks

@julielerman
Copy link

this is my sad little magic string, not type-checked, workaround:
foreach (var entity in modelBuilder.Model.GetEntityTypes()) {

    modelBuilder.Entity(entity.Name).Ignore("thenonmappedproperty");
  }

modelBuilder.Entity().Ignore(p=>p.thenonmappedproperty)

But okay for my purposes for the time being. :)

@AdrianHostettler
Copy link
Author

AdrianHostettler commented Jun 17, 2016

Hy guys, thx for the infos and hints.

My focus isn't on the softdeletion topic, it's more an example of a usecase (like DateCreated/DateModified) for the Filter request.

I've got a working softdeletion implementation by overwriting OnSaveChanges now, but since you both are referring to the shadow properties, i'm wondering if shadow props would be a 'cleaner' approach than overwriting OnSaveChanges.

As I understand the Pros for "shadow props" over "OnSaveChanges Overwriting" would be, that i don't have to implement the same interface on every model, and would have still access to the properties over the "not type-checked" way, as @julielerman describes it in her last comment. So the "not type-checked" way would in that aspect still be a little Con over the "OnSaveChanges" approach.

@julielerman I grew fond of your pluralsight vids the last half year, and so I'm wondering to which approach your experienced guts feeling would tend to?

Edit: Decided to go with Shadow Props, because "DateModified" alike props would get included each time in default scaffolding scenarios. (when not exluding them especially or using custom scaffolding libs)

@anpete
Copy link
Contributor

anpete commented May 8, 2017

Addressed in b1379b1

@anpete anpete closed this as completed May 8, 2017
@anpete anpete added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label May 8, 2017
@anpete anpete modified the milestones: 2.0.0-preview1, Backlog May 8, 2017
@ajcvickers ajcvickers changed the title Base Query Filter (Defining Query) Query: Model-level filters for queries May 9, 2017
@hikalkan
Copy link

Is there any documentation for that?

@HappyNomad
Copy link

Such filters are automatically applied by the query compiler to any query targeting the corresponding EntityType.

To clarify, the filters are also applied to types deriving from EntityType, right?

@divega
Copy link
Contributor

divega commented May 18, 2017

@hikalkan we still have a lot of work to do in documenting 2.0 features. There is a simple example in the announcement blog post.

@HappyNomad that is correct. The only limitation AFAIK is that the filter can only be defined over the base type on a hierarchy.

@HappyNomad
Copy link

HappyNomad commented Oct 16, 2017

Do shadow properties work in this context? I'd like to write:

modelBuilder.Entity<Account>().HasQueryFilter( a => EF.Property<int>( a, "TenantID" ) == TenantID );

If not, where's the issue for this or do I need to create it?

@anpete
Copy link
Contributor

anpete commented Oct 16, 2017

@HappyNomad Yep, that should work.

@HappyNomad
Copy link

HappyNomad commented Oct 18, 2017

Moved to #10117.

@smitpatel
Copy link
Contributor

If that filter is not using navigation path then it would just work.

@ajcvickers ajcvickers modified the milestones: 2.0.0-preview1, 2.0.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

9 participants