实体框架代码首先支持存储过程? [英] Does Entity Framework Code First support stored procedures?

查看:82
本文介绍了实体框架代码首先支持存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过几个EF Code First的演示文稿,没有看到EFCF如何与存储过程一起使用。



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



另外,如果我更改模型,会发生什么?在模型中重新创建表时会丢弃sp吗?触发器呢?



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

解决方案

编辑:我原来的EF4.1(以下)答案现已过期。请参阅来自Diego Vega 的答案(谁在Microsoft的EF团队工作)!






@gsharp和Shawn Mclean:你在哪里得到这些信息?您是否仍然可以访问基础的ObjectContext?

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

将select语句替换为存储的proc,然后再去。



对于你的另一个问题:是的,不幸的是你的sp会被打乱。您可能需要在代码中添加CREATE PROCEDURE语句。



对于EF 4.2:

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


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

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?

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?

解决方案

EDIT: 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 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");

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

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.

For EF 4.2:

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

这篇关于实体框架代码首先支持存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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