Does Entity Framework Code First support stored procedures?

frennky picture frennky · Jan 30, 2011 · Viewed 92k times · Source

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?

Answer

anon picture anon · Jan 30, 2011

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