I am using EF code first approach and I have created stored procedure using migration as following:
public override void Up()
{
Sql(@"CREATE TYPE IdsList AS TABLE
(
Id Int
)
GO
Create Procedure getChildIds(
@IdsList dbo.IdsList ReadOnly
)
As
Begin
WITH RecursiveCTE AS
(
SELECT Id
FROM dbo.PhysicalObjects
WHERE ParentId in (Select * from @IdsList)
--Where Id=108
UNION ALL
SELECT t.Id
FROM dbo.PhysicalObjects t
INNER JOIN RecursiveCTE cte ON t.ParentId = cte.Id
)
SELECT * FROM RecursiveCTE
End");
}
public override void Down()
{
Sql(@"Drop Type IdsList
Go
Drop Procedure getChildIds");
}
Now If I go to sql server management studio and execute the following scripts:
Declare @Ids dbo.IdsList
Insert into @Ids
SELECT 1
Exec getChildIds @Ids
It will execute successfuly, but Now I am trying to execute that stored procedure as following:
var idsList = new SqlParameter {ParameterName = "idsList", Value = new int[] { 1,2,3,4,5} };
var idParams = new SqlParameter("idParams", SqlDbType.Structured)
{
Direction = System.Data.ParameterDirection.Output
};
var results = dbContext.Database.SqlQuery<int>("getChildIds @idsList, @idParams out", idsList,idParams) ;
var idsResult = (List<int>)idParams.Value;
It does not return anything.
So how I could execute stored procedure with input and output parameters of type Table?
You will have to obtain your data a different way:
You cannot return data in a table-valued parameter. Table-valued parameters are input-only; the OUTPUT keyword is not supported.
I have solved it this way.
First of all I have updated my stored procedure to return Ids as following:
public override void Up()
{
Sql(@"CREATE TYPE IdsList AS TABLE
(
Id Int
)
GO
Create Procedure getChildIds(
@IdsList dbo.IdsList ReadOnly
)
As
Begin
WITH RecursiveCTE AS
(
SELECT Id
FROM dbo.PhysicalObjects
WHERE ParentId in (Select * from @IdsList)
UNION ALL
SELECT t.Id
FROM dbo.PhysicalObjects t
INNER JOIN RecursiveCTE cte ON t.ParentId = cte.Id
)
SELECT Id From RecursiveCTE
End");
}
public override void Down()
{
Sql(@" Drop Procedure getChildIds
Go
Drop Type IdsList
");
}
And here how I have solved executing stored procedure using entity framework:
var dataTable = new DataTable();
dataTable.TableName = "idsList";
dataTable.Columns.Add("Id", typeof(int));
dataTable.Rows.Add(1);
dataTable.Rows.Add(2);
SqlParameter idsList = new SqlParameter("idsList", SqlDbType.Structured);
idsList.TypeName = dataTable.TableName;
idsList.Value = dataTable;
var results = dbContext.Database.SqlQuery<int>("exec getChildIds @idsList", idsList).ToList();
I hope my code will help others having the same issue
The best solution for using user's procedures/functions and build-in functions in SQL is this library - https://github.com/Dixin/EntityFramework.Functions
It has great and easy implementation. Also it allows to follow code first approach
In your case you need to use Table-valued function
Define model
[ComplexType]
public class IdModel
{
public int Id { get; set; }
}
Declare Type and Convention in DbContext
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(new FunctionConvention<DbContext>());
modelBuilder.ComplexType<IdModel>();
base.OnModelCreating(modelBuilder);
}
Define function in DbContext
[Function(FunctionType.TableValuedFunction, nameof(ufnGetChildIds), Schema = "dbo")]
public IQueryable<IdModel> ufnGetChildIds([Parameter(DbType = "IdModel", Name = "IdsList")]List<IdModel> IdsList)
{
ObjectParameter IdsListParameter = new ObjectParameter("IdsList", IdsList);
return this.ObjectContext().CreateQuery<IdModel>(
$"[{nameof(this.ufnGetChildIds)}](@{nameof(IdsList)})", IdsListParameter);
}
And use it from your DbContext
[TestMethod]
public void CallTableValuedFunction()
{
using (DbContext database = new DbContext())
{
IQueryable<IdModel> employees = database.ufnGetChildIds(new List<IdModel> { new IdModel { Id = 1 }});
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.