-
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 throws NullReferenceException when global query filter uses null collection on the context #9791
Comments
Trying to write a test for this too if it is fixed by #9825 |
Included a test in #9837 The issue here is in client code. The query filter is appending additional where clause. But in a fresh context instance the value of TennantIds is null hence |
@tplooker Were you seeing a null reference exception? |
@ajcvickers I was getting a NRE on the actual DBSet defined in the dbcontext when I tried to access it after onmodelcreating() so I'm assuming the bad global query filter caused the entity to not load entirely? |
@tplooker Can you post the stack trace? (Essentially, if TennantIds is null when the query is executed, the a null ref exception seems reasonable. However, if it is failing silently, then that seems like something we need to dig into further.) |
So this is the stack trace I get when running a seed method over the database for entity tennantcategory on startup after onModelCreating() with the tennantID list uninitialized. at lambda_method(Closure , TennantCategory ) I would have just expected the NRE to originate from onModelCreating() when it was first applying the global query filter featuring an un-initialized list? |
@tplooker Closing this as by-design. Specifically:
|
@ajcvickers Ok thanks, does this mean that if the tennantId list is set after onmodelcreating, the global query filter shouldnt fail and hence the entity would still load? |
@tplooker Yes, that is my understanding. |
I've just tested it and seen that it does, thanks for clarifying! |
I was using the global query filters for a multi-tennanted system and I noticed if the lambda expression involved in the query fails due to say the TennatId's list being null, then it fails to load the Entity entirely. It was easily solved by initializing the tennantid list before OnModelCreating() but I wondered if there was a way to improve this behavior?
protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Project>().HasQueryFilter(e => !e.IsDeleted && TennantIds.Contains(e.TennantId) && e.IsActive); }
The text was updated successfully, but these errors were encountered: