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

GET LIST IN SESSION OnModelCreatingPartial #23251

Closed
Galandre99 opened this issue Nov 10, 2020 · 2 comments
Closed

GET LIST IN SESSION OnModelCreatingPartial #23251

Galandre99 opened this issue Nov 10, 2020 · 2 comments
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported

Comments

@Galandre99
Copy link

Hi, I need to use method 'HasQueryFilter' based on user permission. I actually done this

IEnumerable<int?> _establishmentIdList = _httpContext.Session.Get<IEnumerable<int?>>("establishmentId");


            if (_establishmentIdList != null && _establishmentIdList.Count() > 0 && _establishmentIdList.FirstOrDefault().HasValue)
            {
                modelBuilder.Entity<RegEmployee>().HasQueryFilter(f => _establishmentIdList.Contains(f.EstablishmentId));

                modelBuilder.Entity<RegProduct>().HasQueryFilter(f => f.RelProductEstalishments.Any(a => _establishmentIdList.Contains(a.EstablishmentId)));

                modelBuilder.Entity<RegEstablishment>().HasQueryFilter(f => _establishmentIdList.Contains(f.Id));
            }

but session is always empty. Any idea how to get session after login?

Thanks all.

EF Core version: 3.0

@ajcvickers
Copy link
Member

@Galandre99 EF Core typically builds the model once and then caches it. This is required for performance. This means that your model building code is only being called for the first DbContext instance, and then never again.

Instead you should make establishmentId a field on your DbContext. This allows the same query filter to be used with different DbContext instances, taking the establishmentId from that instance.

See https://review.docs.microsoft.com/en-us/ef/core/querying/filters
Also, we have an issue open to make this clearer: dotnet/EntityFramework.Docs#773
And an issue open to improve the experience with query filters: #21459

Finally, just an FYI that EF Core 3.0 is out-of-support.

@ajcvickers ajcvickers added the closed-no-further-action The issue is closed and no further action is planned. label Nov 10, 2020
@Galandre99
Copy link
Author

@ajcvickers thank you so much. I resolved it.

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-no-further-action The issue is closed and no further action is planned. customer-reported
Projects
None yet
Development

No branches or pull requests

2 participants