Can't get function mapping to work

Aug 24, 2015 at 9:46 PM
Edited Aug 24, 2015 at 9:47 PM
I've been trying to get a TVF mapped with no success.

I have a function in my DbContext defined as:
    [DbFunction("CodeFirstDatabaseSchema", "CustomerBudgetPeriod")]
    public IQueryable<Period> CustomerBudgetPeriod(int customerId, DateTime date)
    {
        throw new NotImplementedException();
    }
I want to make use of this within another query (I have no intention of calling it directly), so I haven't provided an implementation. "Period" is an existing entity in the same context. I've also included the appropriate convention in the model creation.
However, I get the following exception, without fail:

The specified method ... cannot be translated into a LINQ to Entities store expression because its return type does not match the return type of the function specified by its DbFunction attribute.

Stepping through the code, everything look ok, from what I can tell.
Also, I'm using EF 6.1.3.
Any suggestions?
Aug 31, 2015 at 5:49 AM
I don't see anything wrong with your usage of the DbFunctionAttribute so it's hard to tell exactly what is wrong without more details. From the error you are getting it looks to me that for some reason EF is not recognizing the Period entity. Post a minimal but complete repro and I can try to take a look at it.

It's been a while since I look into EF codebase so I might be wrong but I don't think you can just throw a NotImplementedException from the method created for a TVF. As far as I remember it was fine to have a method that throws for Model Defined Functions and scalar UDFs but I think the expectation is that for a TVF you need to return an IQueryable.

Thanks,
Pawel
Sep 10, 2015 at 4:51 PM
Hey, thanks for your response, and I'm really sorry for the very long delay in replying.
Unfortunately, I changed directions with this, cause we were on a short time frame, and I haven't had a chance to go back on work on this again.
As for the NotImplementedException - somewhere else online, I saw someone doing that. In theory, the method is never executed, as it is just translated into SQL, at least if the function is being used within an outer query (such as in a Where clause).
However, if the function is queried directly (context.MyFunction().Where(f => blah)), then it works correctly provided the context method has a body that executes the correct sql. In that case, the sql is being directly created "by hand" within the context method.
In any case, I did add a full implementation to the method at some point, and it didn't help (at least in the case where it's being used within another query).
Does all that make sense? I need to get some example code put together, but I'll need to find some time for that. I'm in the midst of something else right now :)
-Mark-
On Mon, Aug 31, 2015, at 12:49 AM, moozzyk wrote:

From: moozzyk

I don't see anything wrong with your usage of the DbFunctionAttribute so it's hard to tell exactly what is wrong without more details. From the error you are getting it looks to me that for some reason EF is not recognizing the Period entity. Post a minimal but complete repro and I can try to take a look at it.
It's been a while since I look into EF codebase so I might be wrong but I don't think you can just throw a NotImplementedException from the method created for a TVF. As far as I remember it was fine to have a method that throws for Model Defined Functions and scalar UDFs but I think the expectation is that for a TVF you need to return an IQueryable.
Thanks,
Pawel

Read the full discussion online.

To add a post to this discussion, reply to this email ([email removed])

To start a new discussion for this project, email [email removed]

You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe on CodePlex.com.

Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at CodePlex.com

--
Mark Henry
CLT Hokie
--_----------=_144190026431598390--
Sep 15, 2015 at 5:02 AM
If you give me a repro (code) I can take a look.

Thanks,
Pawel