-
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
Db functions literal mappings #10241
Comments
You would need to provide custom translation using |
Sql Server actually can deal with the literal if it is in double quotes. In this case you can just register the method normally and pass in the datapart as a string. |
Here is my query:
Db function:
Registration in OnModelCreating:
This query throws the following exception:
|
SqlServer can deal with double quotes but double quotes gets translated to builder.HasDbFunction(typeof(SqlFunctions)
.GetMethod(nameof(SqlFunctions.DateDiff)))
.HasTranslation(args =>
{
var newArgs = args.ToList();
newArgs[0] = new SqlFragmentExpression((string)((ConstantExpression)newArgs[0]).Value);
return new SqlFunctionExpression(
"DATEDIFF",
typeof(int),
newArgs);
}); Above worked for me. |
@smitpatel your solution works perfectly, thank you. |
@smitpatel - my bad. I forgot that escape was happening. Trying to multitask too much :) |
@smitpatel sorry for dub question but where did you get the ref to Using |
@ggirard07 - SqlFunctions.DateDiff is defined in user code as written in this post #10241 (comment) |
Hi
In EF Core 2.0 we got a great new feature: support calling db functions.
But there is a problem: how do I have to map SQL literals to C# types?
For example, I can't write C# code that will be translated to something like this:
DATEDIFF(year, '2005-12-31 23:59:59.9999999', '2006-01-01 00:00:00.0000000')
because year is SQL literal.
I suppose it should be some wrapper around string, so that we can easily create instances of it as well as translate to SQL via query providers.
The text was updated successfully, but these errors were encountered: