【发布时间】:2016-05-19 07:20:36
【问题描述】:
我有一个需要表值参数作为参数 (@c) 的 DB 函数。
CREATE TABLE Test
(
CD varchar(10) not null
)
GO
INSERT INTO Test VALUES ('TEST')
GO
CREATE TYPE [CdTable] AS TABLE (CD varchar(10));
GO
CREATE FUNCTION TestTbl ( @x varchar(10), @c CdTable READONLY )
RETURNS TABLE
AS
RETURN
SELECT t.CD
FROM test t
JOIN @c c ON t.CD = c.CD OR c.CD IS NULL
WHERE t.CD = @x
GO
DECLARE @tt AS CdTable;
INSERT INTO @tt VALUES ('TEST');
SELECT * FROM TestTbl('TEST', @tt);
DELETE FROM @tt;
INSERT INTO @tt VALUES (NULL);
SELECT * FROM TestTbl('TEST', @tt);
GO
函数是从 EF 设计器(数据库优先)构建的,就像在 DbContext 中一样:
[DbFunction("MyDbContext", "TestTbl")]
public virtual IQueryable<TestTbl_Result> TestTbl(Nullable<System.String> x)
{
var xParameter = user.HasValue ?
new ObjectParameter("x", x) :
new ObjectParameter("x", typeof(System.String));
return ((IObjectContextAdapter)this).ObjectContext.CreateQuery<TestTbl_Result>("[MyDbContext].[TestTbl](@x)", xParameter);
}
如果我调用这个函数只传递可用的 x/@x 参数,我会得到这个异常:
ex {"An error occurred while executing the command definition. See the inner exception for details."} System.Exception {System.Data.Entity.Core.EntityCommandExecutionException}
ex.InnerException {"An insufficient number of arguments were supplied for the procedure or function TestTbl."} System.Exception {System.Data.SqlClient.SqlException}
我不知道如何将@c 参数传递给函数。有人可以帮忙吗?
提前致谢。
p.s.:我使用的是 MS SQL 2012(或更新版本)
【问题讨论】:
-
应该是DataTable吧?
标签: c# sql-server entity-framework sql-server-2012 entity-framework-6