实体框架可选参数? [英] Entity Framework with optional parameters?
问题描述
使用实体框架5可以使用可选参数的存储过程,以便您不必为每个未使用的参数添加null?
Using Entity Framework 5 is it possible to use a stored proc with optional parameters such that you do not have to add a null for each unused parameter?
存储proc我必须使用87个参数,其中只有2个是必需的。我真的很讨厌在每个电话中放置85个空白的想法。
The stored proc I have to use has 87 parameters only 2 of which are required. I really hate the idea of having to put 85 nulls in each call.
推荐答案
你可以使用 ObjectContext.ExecuteFunction 方法( DbContext
是 ObjectContext
)执行一些存储过程并传递参数列表:
You can use ObjectContext.ExecuteFunction method (DbContext
is a wrapper over ObjectContext
) to execute some stored procedure and pass list of parameters:
FooEntities db = new FooEntities();
var objectContext = ((IObjectContextAdapter)db).ObjectContext;
// create all parameters you need
var name = new ObjectParameter("Name", "Lazy");
var age = new ObjectParameter("Age", 29);
// call stored procedure with these two parameters only
var result = objectContext.ExecuteFunction<Result>("ProcedureName", name, age);
您可以将此代码包装到您的 DbContext
You can wrap this code into extension method for your DbContext
:
public static Result ProcedureName(this FooEntities db, name, age)
{
// code above
}
使用方式如下: var result = db.ProcedureName(Lazy,29);
这篇关于实体框架可选参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!