-
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
Scalar function mapping - does not have default schema #9663
Comments
2.0 does not use the default schema. That was something which was missed. The functionality has been added to 2.1 See #9214 For now you will have to set the schema for each function. Your function should be picked up with just the The only times you have to use Can you confirm that adding the schema to |
Hi @pmiddleton, Thanks for you your explanation. I tried adding the schema in I therefore think the Fluent API command in my original comment is the best solution, i.e. modelBuilder.HasDbFunction(() => AverageVotesUdf(default(int)));
.HasSchema("dbo"); |
Hi @pmiddleton, Can I ask another question? I am trying to understand the |
@JonPSmith - Try |
The main uses case is for system functions which have some quirks that need help working around. Take a look at #9549 for an example of how |
Hi @pmiddleton, Yep, Thanks for the link to #9549 - that is exactly what I needed. I'm not sure I will include that in the book, as it is pretty deep :). PS. @pmiddleton - I would appreciate proof-reading my section in my book on scalar function mapping. I don't want to share my email here but if you go to my web site contact page and send me your email I can get a copy out to you. |
Where can I find a full example of how to map a user defined function? All I get is a big fat exception when I try to run it. What's with the method throwing an exception? It doesn't look like a very intuitive pattern. |
Jon's example on this post works. What exception are you getting? What does your code look like? |
My test UDF: In my data context: In OnModelCreating: |
The method must be static. You also don't need to use both types of registration. |
Okay so I changed the method to static: Then I removed the registration in OnModelCreating but I still get an exception. |
You're going to need to post a repo example at this point. What exception are you getting? |
I'll have to work on creating an example. The exception is the one in my method body of course. Why is the pattern to throw an exception? Obviously I don't know how it works underneath but on the surface the pattern doesn't make any sense. |
It sounds like you have a query which can't be fully translated and you are running into client side evaluation of the function. Have a look at this article for more help. https://docs.microsoft.com/en-us/ef/core/querying/client-eval |
Oh wow that's a cool new feature and now I get it. Turns out to test it I was calling the method directly and not in a LINQ query. Which leads me to my next question lets say I want to just call a scalar function is there anyway to wrap those and call them directly yet? Same thing with stored procedures, is there anyway to call those without using FromSql on a DbSet? |
Currently you can't call the UDFs directly. I'm currently working on that feature. See #9810 The same holds true for stored procs. IDK if there is a feature request for calling stored procs directly. |
I am writing about scalar function mapping and I found that I had to define a schema of "dbo" for EF Core to find the user defined function (UDF). Is this because of the way I defined the UDF or doesn't the configuration of the
HasDbFunction
method pick up the default schema?Also, the EF Core New inEF Core 2 documentation doesn't tell you that you need to use the
HasDbFunction
method to tell EF Core about the UDF, although @pmiddleton announcement comment does.I get the following exception when I don't define the schema.
Steps to reproduce
My unit test has a setup stage, which adds the UDF and seeds the database if a new database is created.
My
AddUdfToDatabase
method looks like this:My DbContext looks like this.
Note: I have commented out the
HasSchema
method - if that is applied then it works.Further technical details
EF Core version: EF Core 2.0.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.3
The text was updated successfully, but these errors were encountered: