-
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
Adding a custom function for server side execution using SqlFragment #23375
Comments
|
Yes but what exactly am I doing wrong here? Why it's not able to identify and do the translation? |
There can be a lot of things which would be going wrong. The very least, following a post about 2.1 version in version 5.0 which has changed quite a lot internally. Only customer facing APIs and UX were maintained, nothing internal. We would be glad to help you out if things are not working for supported scenario. Since this is not a supported use-case, you shouldn't be using it. |
I'm not using 5.0 but the version is 3.1.5. Stakeholders do not want to migrate to 5.0 now. My objective is to execute this condition on the server side.
I don't want to bring those millions of records on client side and also sometimes I want to apply condition on the server side so that only necessary data is received. so what you say scalar values, that means a string or number that should be passed to IsDeleted function? You see it's not really about a particular value but for a column against which I need filtering. |
Okay, let me try this but in this case I need to define Query Filter for every entity and we have almost a large number of them. |
I looked at the referenced article and all the code above, there are multiple errors in both article and above user attempt. Let me try to describe all of them.
A right way to do this translation would be just use like normal property. // class
public class Employee : IActiveEntity
{
public bool IsDeleted {get; set; } // Mapped to IsDeleted column in the table
// other properties on the entities.
}
// In the query
var query = context.Employees.Where(e => !e.IsDeleted).Max(e => e.Age); You can apply IsDeleted filter directly in the query when using it without using any custom function mapping or any additional code just by mapping the property to the column you already have in the database. |
Problem:
I'm using EFCore 3.1 ( a project recently migrated from an older version) where I have to retrieve records based on if they are soft deleted or not. Any soft deleted record is not considered for any operation performed on the table.
Soft deleted is implemented in a form of IsDeleted table column which is a bit. 1 = soft deleted, 0 = row is available. In the C#, there is an interface IActiveEntity with a bool property of IsDeleted
Certain operation implemented in generic form and therefore they check if the entity is of type IActiveEntity. I read a series of articles. https://www.thinktecture.com/en/entity-framework-core/custom-functions-using-hasdbfunction-in-2-1/
But it's not working quite the way as mentioned in article. There is also not enough documentation available.
I implemented the function extensions for EF.Functions:
Then I created corresponding class to translate the expression into correct sql code.
Then the method call translator for the IsDeleted expression:
A class to register the Method call translator
After this these two classes to add to DBContext
And this is how add it to DbContext
When I run the code it calls the Extension method IsDeleted and throws exception instead of performing any sort of translation. What am I missing so that EF Core start recognizing the call to be translated into SQL and not execute the local function.
Update:
This is how I use the code:
this is the error I get:
The LINQ expression
could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync(). See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
The text was updated successfully, but these errors were encountered: