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

发布于 2024-10-14 02:35:32 字数 768 浏览 1 评论 0原文

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

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

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

这是ObjectContext 上的一个方法。

但是DbContext没有这样的方法。

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

编辑:

我发现

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

这引起了一些问题:

1) 您现在正在集合上调用存储过程,而不是上下文嗯>。存储过程应该在上下文范围内可用,而不是绑定到特定的实体集。就像它们在 SQL Server 中的“数据库”下,而不是在“表”下一样。

2)复杂类型怎么样?我以前有一个从存储过程返回的复杂类型。但现在,看起来好像你必须直接映射到一个实体?这没有任何意义。我有许多存储过程返回一个不直接由 ObjectSet/DBSet 表示的类型,我不知道如何将其拉过来。

希望有人能为我解决这个问题,因为据我所知,我将无法升级到 CTP5。

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

In Entity Framework 4.0, we did this:

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

Which is a method on the ObjectContext.

But DbContext has no such method.

How do we call a stored proc? Is it not supported in 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".

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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

晚雾 2024-10-21 02:35:32

您可以像这样执行数据库范围的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");
}

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");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文