Chapter 7. Using Stored Procedures with the EDM

Many databases use stored procedures to perform predefined logic on database tables, and many organizations have policies in place that require the use of these stored procedures. Although one of the key features of the Entity Framework is its ability to automatically build native commands based on your LINQ to Entities or Entity SQL queries, as well as build the commands for inserting, updating, or deleting data, you may want to override these steps and use your own predefined stored procedures. Although the dynamically built commands are secure, efficient, and generally as good as or better than those you may write yourself, there are many cases where stored procedures already exist and your company practices may restrict direct use of the tables. Alternatively, you may just want to have explicit control over what is executed on the store and prefer to create stored procedures.

The sample database includes six stored procedures that we skipped in our discussion of model creation in Chapter 2. In this chapter, you will update the model, pulling in those six stored procedures, implementing them in the model, and interacting with them in some code.

In this chapter, you will override the Entity Framework’s command generation feature for a particular entity and direct it to use your stored procedures instead when SaveChanges is called. You’ll also learn how to incorporate and use procedures that return data. The chapter will also address ...

Get Programming Entity Framework, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.