I am trying to add the DATEPART sql-server function to Entity Framework Core by adding it as a static method on DbContext with the [DbFunction] attribute as described here https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0
The problem is that sql-server receives the datepart parameter as a string and it cant run it because the datepart parameter can't be a string (based on https://learn.microsoft.com/en-us/sql/t-sql/functions/datepart-transact-sql?view=sql-server-2017 "Note DATEPART does not accept user-defined variable equivalents for the datepart arguments.")
i have also tried removing the double quotes from the argument i pass to datepart, by doing sting.Replace("\"", "") but it still does not work (it change the argument from "'arg'" to 'arg')
Here is my code:
[DbFunction("DATEPART","")]
public static int DATEPART(string datepart, DateTime date)
{
throw new NotSupportedException();
}
Is there any other data type that will work? am i missing something?
Thanks