question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Map inserts, updates, and deletes (CUD operations) to stored procedures

See original GitHub issue

Note: Some of the features tracked in this issue could help with using EF Core with database views. However, the feature is not limited to database views and its completion would not mean that every aspect of database view support has been implemented. See #827 for an overview of the areas where EF Core interacts with database views.


Feature description

(added by @divega on Sep 4, 2014)

This is a general meta-issue for enabling better support for working with stored procedures in EF Core, with an emphasis on mapping stored procedures to parts of the EF Core model as opposed to using them in an ad hoc manner, which is covered in other issues. There are different aspects of stored procedures support than should be consider separately, e.g.:

  • Invoking stored procedures in an ad hoc manner to return entity types (already possible with FromSql)
  • Invoking stored procedures in an ad hoc manner to return arbitrary types (covered by #1862)
  • Wrap Migrations calls to create stored procedures on SQL Server with EXEC (see #19740)
  • Mapping of CUD operations to stored procedures

Backlog

Note that for data retrieval stored procedures aren’t generally considered composable on the store i.e. it wouldn’t normally be possible to reference them in LINQ queries without causing all the further processing to switch to the client (#621 and #622 cover composable functions) but with our compensating LINQ provider it could be possible some day to enable mapping specific LINQ patterns to stored procedures calls, e.g.:

var customerOrders = db.Orders.Where(o => o.Customer == customerId);

Could cause a stored procedure OrdersByCustomerId(int @customerId) to be invoked.


API proposal

(added by @AndriySvyryd on May 6, 2022)

modelBuilder.Entity<Customer>()
    .UpdateUsingStoredProcedure(u => u.HasName("modify_cust")
        .HasParameter(b => b.Id, pb => pb.HasName("customer_id"))
        .HasParameter(b => b.Name, pb => pb.IsOutput())
        .HasParameter(b => b.AlternateKey)
        .HasRowsAffectedParameter(pb => pb.HasName("rows_affected")))
    .DeleteUsingStoredProcedure(d => d.HasName("delete_cust")
        .HasParameter(b => b.Id, pb => pb.HasName("customer_id")))
    .InsertUsingStoredProcedure(i => i.HasName("insert_cust")
        .HasParameter(b => b.Id, pb => pb.HasName("customer_id"))
        .HasParameter(b => b.Name)
        .HasParameter(b => b.AlternateKey)
        .HasResultColumn(b => b.Id, rb => rb.HasName("customer_out_id"))
        .HasResultColumn(b => b.Name)
        .HasRowsAffectedParameter(pb => pb.HasName("rows_affected"))
        .SuppressTransactions());

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:119
  • Comments:85 (21 by maintainers)

github_iconTop GitHub Comments

19reactions
mathewsuncommented, Feb 11, 2020

6 years have passed! How many time we should wait this functionality?!!! It main functionality for rapid development.

16reactions
IlyaTsilikovcommented, Dec 8, 2014

IMHO one of the most useful features of EF 6 is wrapping stored procedure into DbContext-based class methods. So it would be better to include such feature in EF 7 and if it’s possible make two improvemtnts: 1). make available EF to generate methods with out-parameters for OUTPUT SQL keyword (and also ref); 2). make EF available to generate async/await verson of such methods.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stored Procedure Mapping in EF 6 Code-First
You must map insert, update and delete stored procedures to an entity if you want to use stored procedure for CUD operations. Mapping...
Read more >
Designer CUD Stored Procedures - EF6
This step-by-step walkthrough show how to map the create\insert, update, and delete (CUD) operations of an entity type to stored procedures ...
Read more >
Mapping CUD Operations to Stored Routines - Documentation
Using stored routines for insert/update/delete operations is a widely met task in ... Right-click the Dept class and then click Stored Procedure Mapping....
Read more >
Entity Framework CRUD Operations Using Stored Procedure
By mapping Stored Procedures to the Model. OK, first, we will see how to map the Stored Procedures to the Model to implement...
Read more >
Entity Framework mapping insert, update and delete stored ...
To map the insert, update and delete stored procedure to the Post insert/update and delete operations, click the Post entity and click ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found