实体框架CTP5-如何调用存储过程? [英] Entity Framework CTP5 - How to Call Stored Procedure?
问题描述
这可能是一个简单的答案,但是我看不到如何使用EF CTP5执行存储过程.
This may be a simple answer, but i can't see how to execute a stored procedure with EF CTP5.
在Entity Framework 4.0中,我们这样做:
In Entity Framework 4.0, we did this:
ExecuteFunction("ContainerName.StoredProcName", new ObjectParameter("Id", id))
.
这是ObjectContext
上的一种方法.
但是DbContext
没有这种方法.
我们如何调用存储的proc? EF CTP5不支持吗?
How do we call a stored proc? Is it not supported in EF CTP5?
我找到了此线程,表明您需要执行以下操作:
I found this thread, which states you need to do this:
var people = context.People.SqlQuery("EXECUTE [dbo].[GetAllPeople]");
这引起了一些担忧:
1)您现在正在调用 set 上的存储过程,而不是 context .存储过程应该在上下文范围内可用,而不是与特定实体集绑定在一起.就像它们在SQL Server中的数据库"下而不是在表"下一样.
1) You are now calling a stored prodedure on the set, not the context. Stored procedures should be available context-wide, not tied to a particular entity set. Just like how they are under the "Database" in SQL Server, and not under the "Table".
2)复杂类型呢?我以前有一个从存储过程返回的复杂类型.但是现在看来,您似乎必须直接映射到实体?那没有任何意义.我有很多存储的proc,它们返回的类型不是由ObjectSet/DBSet直接表示的,我看不到如何套用.
2) What about complex types? I previously had a complex type being returned from a stored procedure. But now, it looks as though you have to map directly to an entity? That doesn't make any sense. I have many stored procs that return a type not directly represented by an ObjectSet/DBSet, which i can't see how i can pull over.
希望有人可以帮我解决这个问题,因为据我了解,我将无法升级到CTP5.
Hope someone can clear this up for me, because from what i understand so far, i won't be able to upgrade to CTP5.
推荐答案
您可以像这样执行数据库范围的sql语句
You can execute database-wide sql statements like this
using(var context = new MyContext())
{
// custum sql statement
var c = context.Database.SqlQuery<int>("SELECT COUNT(*) FROM Employees");
// returned entity type doesn't have to be represented by ObjectSet/DBSet
var e = context.Database.SqlQuery<Employee>("SELECT * FROM Employees");
// stored procedure
var q = context.Database.SqlQuery<Employee>("GetEmployees");
}
这篇关于实体框架CTP5-如何调用存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!