Entity Framework Pitfalls: Using Stored Procedures for Basic Operations

Entity Framework, as of version 6, allows the usage of stored procedures for its CUD (Create, Update and Delete) operations. We just need to override the DbContext’s OnModelCreating method and call MapToStoredProcedures.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Master>().MapToStoredProcedures();
 
    //rest goes here
 
    base.OnModelCreating(modelBuilder);
}

The problem is, this is all or nothing: you cannot use a stored procedure for just one of the operations, you need to use stored procedures for all operations. And, no, it is not possible (yet) to use custom SQL, just stored procedures.

                             

3 Comments

  • But you're posting on September 29, 2015, and the Release Candidate of Entity Framework 7 will be delivered in a month or so, with the following:

    o Use LINQ to retrieve data from the database.

    o Mixed client/server evaluation enables queries to contain logic that cannot be evaluated in the database, and must therefore be evaluated after the data is retrieved into memory.

    o NoTracking queries enables quicker query execution when the context does not need to monitor for changes to the entity instances (i.e. the results are read-only).

    o Eager Loading provides the Include and ThenInclude methods to identify related data that should also be fetched when querying.

    o Async query can free up the current thread to process other requests while the database processes the query.

    o Translation of common BCL functions enables these functions to be translated into database specific query language (i.e. SQL) when they are used in LINQ.

    o Raw SQL queries provides the DbSet.FromSql method to use raw SQL queries to fetch data. These queries can also be composed on using LINQ.

  • Hi, EricTN!
    Yes, I know all that, but how does that contradict - or, in fact, have anything to do - with my post?
    :-)

  • The thing I find frustrating overall is my company, a fortune 500 bank, will absolutely never allow anything but stored procedures to be used. Not worth the mount everest climb to try to get them to reconsider.

Add a Comment

As it will appear on the website

Not displayed

Your website