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

Compiled model: Generate code for query filters when possible #24897

Open
Tracked by #21459 ...
AndriySvyryd opened this issue May 13, 2021 · 16 comments
Open
Tracked by #21459 ...

Compiled model: Generate code for query filters when possible #24897

AndriySvyryd opened this issue May 13, 2021 · 16 comments

Comments

@AndriySvyryd
Copy link
Member

Avoid the need for post-processing for relational providers

@AndriySvyryd AndriySvyryd changed the title Compile model: generate code for query filters when possible Compiled model: Generate code for query filters when possible May 13, 2021
@AndriySvyryd AndriySvyryd added this to the Backlog milestone May 18, 2021
@AndriySvyryd AndriySvyryd removed this from the Backlog milestone Jun 8, 2021
@AndriySvyryd AndriySvyryd self-assigned this Jun 23, 2021
@AndriySvyryd AndriySvyryd added this to the 6.0.0 milestone Jun 23, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, Backlog Jul 30, 2021
@stevendarby
Copy link
Contributor

stevendarby commented Nov 17, 2021

Would be interested to know if you have a rough idea of what sort of query filters you might be able to generate code for? Just to get an idea if our model with query filters is likely to be supported in future. The filters are like x.TenantId == this.TenantId && x.SoftDeleted == false

@ajcvickers
Copy link
Member

Note from triage: consider encapsulating filter in a type as a way of not needing the expression tree to be contained in the model.

@ilmax
Copy link
Contributor

ilmax commented Nov 3, 2022

It would be awesome to get more compiled models features in EF 8, this one is in particular since query filters are quite useful, so I tend to use them a lot

@zejji
Copy link

zejji commented Feb 25, 2023

Is there a workaround for this in the interim, i.e. is there a way to use compiled models together with global query filters even if code for query filters is not currently generated? The original post refers to avoiding the need for "post-processing", but I'm not sure what this is referring to.

(I always use soft delete filters, so haven't been able to take advantage of compiled models to date.)

@ajcvickers
Copy link
Member

@zejji I don't believe there is any workaround for this.

@truonghoangsw
Copy link

truonghoangsw commented Aug 9, 2023

We are add globle filter. In Function CreateAnnotations of file EntityType

public static void CreateAnnotations(RuntimeEntityType runtimeEntityType)
{
    runtimeEntityType.AddAnnotation("Relational:FunctionName", null);
    runtimeEntityType.AddAnnotation("Relational:Schema", null);
    runtimeEntityType.AddAnnotation("Relational:SqlQuery", null);
    runtimeEntityType.AddAnnotation("Relational:TableName", "Application");
    runtimeEntityType.AddAnnotation("Relational:ViewName", null);
    runtimeEntityType.AddAnnotation("Relational:ViewSchema", null);
    Expression<Func<Application, bool>> expression = Application => !Application.IsDeleted;
    runtimeEntityType.AddAnnotation(CoreAnnotationNames.QueryFilter, expression);
    Customize(runtimeEntityType);
} 

can i use it like this?
Will it cause an error? :( @ajcvickers

@AndriySvyryd
Copy link
Member Author

@truonghoangsw Yes, that should work as long as the filter doesn't have to reference the DbContext

@tidan-16
Copy link

tidan-16 commented Nov 5, 2023

Would be very nice for ef core 9! Most of the time we would want to use it for multi tenancy and soft deleting

@stevendarby
Copy link
Contributor

Would be very nice for ef core 9! Most of the time we would want to use it for multi tenancy and soft deleting

Unfortunately there is no clear scope defined for what "when possible" means, so these scenarios might not be supported.

@tidan-16
Copy link

tidan-16 commented Nov 8, 2023

truonghoangsw's comment seems to work no?

@truonghoangsw
Copy link

truonghoangsw's comment seems to work no?

Yes,it work

@klemmchr
Copy link

I suppose that this issue is about the limitation of using global query filters in combination with compiled models. The original announcement states that it is not possible to use compiled models with global query filters. Is this still the case? I'm unable to find any first party documentation about compiled models.

I would like to use compiled models with global query filters. Currently we are using them to filter out soft deleted entities. Some comments here mention that this is possible. Is this the case?

@AndriySvyryd
Copy link
Member Author

@klemmchr Yes, as a workaround you can remove the global filter, generate a compiled model and then manually edit it to add the filter as seen in #24897 (comment)

@klemmchr
Copy link

klemmchr commented May 25, 2024

@klemmchr Yes, as a workaround you can remove the global filter, generate a compiled model and then manually edit it to add the filter as seen in #24897 (comment)

Would this change be persisted between model updates and recompilations or do I need to add this manually every time after compiling?

@AndriySvyryd
Copy link
Member Author

Would this change be persisted between model updates and recompilations or do I need to add this manually every time after compiling?

You'd need to do it every time you regenerate the model.

@erdalsivri
Copy link

You can do it in the Customize partial method in a separate file. Compiled model EntityType classes are all partial

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

9 participants