entity framework中的表值函数?

是否可以使用entity framework调用表值函数(TVF)?

我在我的数据库中定义了三个TVF,它们没有显示在Entity Framework的模型中,也没有显示在“从数据库更新模型”向导中。

在Linq-to-SQL中很容易做到这一点,你只需将TVF拖到设计图面上,但在L2E中看起来似乎不太可能。

到目前为止,我还没有找到任何甚至一起提到TVF和entity framework的内容。

如果您只需要在Code-First 4.3中将结果作为类型列表从TVF获得,您可以在DbContext上设置帮助程序,例如

public class ModelDbContext : DbContext { public IEnumerable FunctionTableValue(string functionName, SqlParameter[] parameters) { parameters = parameters ?? new SqlParameter[] { }; string commandText = String.Format("SELECT * FROM dbo.{0}", String.Format("{0}({1})", functionName, String.Join(",", parameters.Select(x => x.ParameterName)))); return ObjectContext.ExecuteStoreQuery(commandText, parameters).ToArray(); } private ObjectContext ObjectContext { get { return (this as IObjectContextAdapter).ObjectContext; } } } 

称之为

 using (var db = new ModelDbContext()) { var parameters = new SqlParameter[] { new SqlParameter("@Id", SqlDbType.Int), }; parameters[0].Value = 1234; var items = db.FunctionTableValue("fn_GetFoos", parameters); }