Table Valued Function and Entity Framework

只谈情不闲聊 提交于 2021-02-06 08:47:18

问题


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.


回答1:


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.




回答2:


[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



回答3:


This is a way to send table parameter to function and return values of table valued function.

C#:

var fooDataTable = new DataTable();
var ids = new List<FooDto>();
if (ids.Count > 0)
{
    fooDataTable.Columns.Add("ID", typeof(int));
    fooDataTable.Columns.Add("CarNumber");
    fooDataTable.Columns.Add("ArriveDate", typeof(DateTime));                

    foreach (var car in ids)
    {
        fooDataTable.Rows.Add(car?.ID, car?.CarNumber, car?.ArriveDate);
    }
}

SqlParameter cdIDs = new SqlParameter("@ListToCalc", SqlDbType.Structured);
cdIDs.Value = fooDataTable;
cdIDs.TypeName = "tp_CarList";

var template = new CarFieldsDTO
{
    Fields = db.Database.SqlQuery<fn_Car_Result>
        ("SELECT * FROM dbo.fn_Car(@ListToCalc)", cdIDs)
            .Select(field => new CarFieldsDTO
            {
                ID = field.ID,
                CarNumber = field.CarNumber,
                ArriveDate = field.ArriveDate,                    
            }).ToList()
};

var fields = new List<CarFieldsDTO> { template };
return fields.AsQueryable();

Where db is DbContext.

DTO:

public class CarFieldsDTO
{        
    public int ID { get; set; }

    public string CarNumber { get; set; }

    public DateTime? ArriveDate { get; set; }

    public IEnumerable<CarFieldsDTO> Fields { get; set; }
}

SQL table valued function:

ALTER  FUNCTION [dbo].[fn_Car]
(
      @ListToCalc tp_CarList READONLY
)
RETURNS TABLE
AS
    RETURN
    (
        SELECT l.ID
               , l.CarNumber
               , l.ArriveDate
        FROM @ListToCalc l
        INNER JOIN Stations as sf ON sf.ID = l.id_StationFrom
    )

User defined Table Type:

CREATE TYPE [dbo].[tp_CarList] AS TABLE(
    [ID] [int] NOT NULL,
    [CarNumber] [varchar](12) NULL,
    [ArriveDate] [datetime] NULL    
)
GO


来源:https://stackoverflow.com/questions/18454578/table-valued-function-and-entity-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!