-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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 filter samples don’t make sense #12375
Comments
@granthoff1107 TenantId is property on the entity type--in this base Blog. While it is backed by a _tenantId field, this is not the same _tenantId field that is used in the filter; the one in the filter is a member of the DbContext class. This one is captured into the query expression such that it can be changed on each context instance resulting the context filtering queries based on whatever _tenantId is set on the current instance. |
How so if you look at the blog class the tenantId is private, it doesn’t look like it’s set anywhere this is unclear from the documentation |
This line creates a shadow property named |
@smitpatel, okay so the tenantId is assigned per say by some authorization service but who makes the assignment to the tenantId in the blog class? |
@granthoff1107 - That is something user has to do. Generally it is done by overriding Like showed in sample We have tracking issue #10862 making it more first class. |
The sample you provided sets the property to equal to the same property used in the DbContext, I still don’t see how this would evaluate to anything but true? |
Maybe I’m confused what tenancy is used for. I thought it was primarily used to filter values by companies, users etc... I don’t understand why would someone one override savechanges to set a field on a value which is only backed by a property? |
@granthoff1107 Yep the example is confusing all right. The use case you suggested is covered in Include the IsDeleted values when user is admin where the field is defined in the DbContext, not the model. |
The comparison of the property is against Here is a fuller example that illustrates it better: public class BlogContext : DbContext
{
private string _tenantId; // This field is what the entity is compared against
public BlogContext(DbContextOptions options)
: base(options)
{
_tenantId = "1"; // Hard-coded in this example case
}
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Blog>().Property<string>("TenantId").HasField("_tenantId");
modelBuilder.Entity<Blog>().HasQueryFilter(b => EF.Property<string>(b, "TenantId") == _tenantId);
}
} The public BlogContext(DbContextOptions options, IHttpContextAccessor httpContextAccessor)
: base(options)
{
_tenantId = httpContextAccessor.HttpContext.User.FindFirstValue("TenantId");
} |
The tenantId queryfilter samples make no sense
The tenantId filter checks to see if the property which backed by _tenantId is equivalent to _tenantId, this should always return true
The text was updated successfully, but these errors were encountered: