Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table Valued Function and Entity Framework

I'm trying to execute an TVF with Entity Framework and for some reason it just doesn't work. Maybe anyone out there can help me see the problem.

Here are the code samples:

That's the function:

CREATE FUNCTION [dbo].[udf_profileSearch]
(@keywords NVARCHAR(3000))
RETURNS @results TABLE 
(
    [Id] [int] NULL,
    [SubCategoryId] [int] NULL,
    [UserId] [int] NULL,
    [SmallDescription] [nvarchar](250) NULL,
    [DetailedDescription] [nvarchar](500) NULL,
    [Graduation] [nvarchar](140) NULL,
    [Experience] [nvarchar](500) NULL,
    [IsChat] [bit] NULL,
    [IsEmail] [bit] NULL,
    [MinuteCost] [decimal](18, 2) NOT NULL,
    [TestimonyRate] [int] NULL,
    [TestimonyQuantity] [int] NULL,
    [StatusId] [int] NULL
)

AS
BEGIN
IF(@keywords != '')
    BEGIN
        insert @results
            SELECT p.Id, p.SubCategoryId, p.UserId, p.SmallDescription, p.DetailedDescription, p.Graduation, 
                        p.Experience, p.IsChat, p.IsEmail, p.MinuteCost, p.TestimonyRate, p.TestimonyQuantity, 
                        p.StatusId FROM 
            Profile p inner join ProfileSearchKeyword psk
            ON p.Id = psk.ProfileId
            WHERE CONTAINS(psk.*,@keywords)
    END
ELSE 
    BEGIN
        insert @results
            SELECT p.* FROM 
            Profile p inner join ProfileSearchKeyword psk
            ON p.Id = psk.ProfileId
    END
RETURN
END

I have this in my DbContext file (named EAjudaContext)

[EdmFunction("eAjudaConnection", "udf_profileSearch")]
    public virtual IQueryable<Profile> udf_profileSearch(string keywords)
    {
        var keywordsParameter = keywords != null ?
            new ObjectParameter("keywords", keywords) :
            new ObjectParameter("keywords", typeof(string));

        return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<Profile>("eAjudaConnection.udf_profileSearch(@keywords)", keywordsParameter);
    }

That's how I'm calling the func via LINQ

var result = from ps in eAjudaCtx.udf_profileSearch("query") select ps

And I get this error:


'eAjudaConnection.udf_profileSearch' cannot be resolved into a valid type or function.

Any ideas on what I'm missing? I've tried pretty much every tip I found on google, but none solved my problem.

If you need to see any piece of code not included here, just ask and I'll add it.

like image 681
BrunoPugliese Avatar asked Aug 26 '13 23:08

BrunoPugliese


People also ask

How do you call a table-valued function in Entity Framework?

Step 1 − Select the Console Application from the middle pane and enter TableValuedFunctionDemo in the name field. Step 2 − In Server explorer right-click on your database. Step 3 − Select New Query and enter the following code in T-SQL editor to add a new table in your database.

What is the difference between EF6 and EF core?

EF 6 is a stable and mature ORM while EF Core is relatively new. Microsoft rebuilt EF Core from the ground up and removed many of the internal dependencies and providers that EF 6 had (like SQLClient). In the long run, that will make EF Core much more extensible and lighter weight.

Is EF core better than EF6?

Keep using EF6 if the data access code is stable and not likely to evolve or need new features. Port to EF Core if the data access code is evolving or if the app needs new features only available in EF Core. Porting to EF Core is also often done for performance.

What is a table-valued function?

A table function, also called a table-valued function (TVF), is a user-defined function that returns a table. You can use a table function anywhere that you can use a table. Table functions behave similarly to views, but a table function can take parameters.


2 Answers

[Tested] using:

Install-Package EntityFramework.CodeFirstStoreFunctions

Declare a class for output result:

public class MyCustomObject
{
   [Key]
   public int Id { get; set; }
   public int Rank { get; set; }
}

Create a method in your DbContext class

[DbFunction("MyContextType", "SearchSomething")]
public virtual IQueryable<MyCustomObject> SearchSomething(string keywords)
{
   var keywordsParam = new ObjectParameter("keywords", typeof(string)) 
                           { 
                              Value = keywords 
                            };
    return (this as IObjectContextAdapter).ObjectContext
    .CreateQuery<MyCustomObject>(
     "MyContextType.SearchSomething(@keywords)", keywordsParam);
}

Add

public DbSet<MyCustomObject> SearchResults { get; set; }

to your DbContext class

Add in the overriden OnModelCreating method:

modelBuilder.Conventions
.Add(new CodeFirstStoreFunctions.FunctionsConvention<MyContextType>("dbo"));

And now you can call/join with a table values function like this:

CREATE FUNCTION SearchSomething
(   
    @keywords nvarchar(4000)
)
RETURNS TABLE 
AS
RETURN 
(SELECT KEY_TBL.RANK AS Rank, Id
FROM MyTable 
LEFT JOIN freetexttable(MyTable , ([MyColumn1],[MyColumn2]), @keywords) AS KEY_TBL      
ON MyTable.Id = KEY_TBL.[KEY]  
WHERE KEY_TBL.RANK > 0   
)
GO
like image 182
Nina Avatar answered Sep 22 '22 18:09

Nina


Here is a very good article on the newer features of Entity Framework that provide direct support for Table Valued UDFs. MSDN blog on Table-Valued Function Support in Entity Framework.

For greater depth, this article provides significant detail. EDM and store functions exposed in LINQ.

One of the great advantages of the recent support for Table-Valued UDFs involves support for Full-Text Search capabilities. Read more about that here: Full text search features involving database objects.

like image 24
devinbost Avatar answered Sep 21 '22 18:09

devinbost