Entity Framework Code First 是否支持存储过程? [英] Does Entity Framework Code First support stored procedures?

查看:33
本文介绍了Entity Framework Code First 是否支持存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个关于 EF Code First 的演示,但还没有看到 EFCF 如何与存储过程一起工作.

I've watched several presentations of EF Code First and haven't seen how EFCF works with stored procedures.

如何声明将使用某些 sp 的方法?我可以将实体传递给调用 sp 的方法,而无需手动将实体属性映射到 sp 参数吗?

How can I declare a method that will use some sp? Can I pass an entity to a method that calls sp without manually mapping entity properties to sp parameters?

此外,如果我更改模型会怎样?从模型重新创建表时它会删除我的 sp 吗?那么触发器呢?

Also, what happens if I change my model? Would it drop my sp while recreating table from model? And what about triggers?

如果不支持这些东西,未来有没有计划支持它们?

If these things are not supported, are there any plans to support them in future?

推荐答案

我对 EF4.1(以下)的原始答案现已过时.请参阅以下来自 Diego Vega 的回答(在 Microsoft EF 团队工作)!

My original answer for EF4.1 (below) is now out of date. Please see the answer below from Diego Vega (who works on the EF team at Microsoft)!

@gsharp 和 Shawn Mclean:你从哪里得到这些信息?难道你还不能访问底层的 ObjectContext 吗?

@gsharp and Shawn Mclean: Where are you getting this information? Don't you still have access to the underlying ObjectContext?

IEnumerable<Customer> customers = 
    ((IObjectContextAdapter)this)
    .ObjectContext.ExecuteStoreQuery<Customer>("select * from customers");

用存储过程替换select"语句,然后就可以了.

Replace the "select" statement with a stored proc, and there you go.

至于您的另一个问题:是的,不幸的是,您的 s.p. 会遭到破坏.您可能需要在代码中添加CREATE PROCEDURE"语句.

As for your other question: Yes, unfortunately your s.p.'s will get clobbered. You may need to add the "CREATE PROCEDURE" statements in your code.

对于 EF 4.2:

var customers = context.Database.SqlQuery<Customer>("select * from customers")

这篇关于Entity Framework Code First 是否支持存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆