实体框架CTP5 - 如何调用存储过程?

时间:2022-05-04 22:02:18

This may be a simple answer, but i can't see how to execute a stored procedure with EF CTP5.

这可能是一个简单的答案,但我看不到如何使用EF CTP5执行存储过程。

In Entity Framework 4.0, we did this:

在Entity Framework 4.0中,我们这样做了:

ExecuteFunction("ContainerName.StoredProcName", new ObjectParameter("Id", id)).

ExecuteFunction(“ContainerName.StoredProcName”,新的ObjectParameter(“Id”,id))。

Which is a method on the ObjectContext.

这是ObjectContext上的一个方法。

But DbContext has no such method.

但是DbContext没有这样的方法。

How do we call a stored proc? Is it not supported in EF CTP5?

我们如何调用存储过程? EF CTP5不支持吗?

EDIT:

编辑:

I found this thread, which states you need to do this:

我找到了这个线程,声明你需要这样做:

  var people = context.People.SqlQuery("EXECUTE [dbo].[GetAllPeople]");

This raises some concerns:

这引起了一些担忧:

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".

1)您现在在集合上调用存储的程序,而不是上下文。存储过程应该在上下文中可用,而不是与特定实体集相关联。就像它们在SQL Server中的“数据库”下,而不是在“表”下。

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.

2)复杂类型怎么样?我之前有一个从存储过程返回的复杂类型。但现在看起来好像你必须直接映射到一个实体?这没有任何意义。我有很多存储过程返回一个不是由ObjectSet / DBSet直接表示的类型,我无法看到我如何能够完成。

Hope someone can clear this up for me, because from what i understand so far, i won't be able to upgrade to CTP5.

希望有人可以为我清除这一点,因为据我所知,到目前为止,我将无法升级到CTP5。

1 个解决方案

#1


9  

You can execute database-wide sql statements like this

您可以像这样执行数据库范围的sql语句

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");
}

#1


9  

You can execute database-wide sql statements like this

您可以像这样执行数据库范围的sql语句

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");
}