locked
Getting SCOPE_IDENTITY using EntityFramework 6+ RRS feed

  • Question

  • I have a bunch of OLD stored procedures that insert values into tables and return nothing.

    AND, I can't change the stored procedures as other code still depends on them, etc...

    So, after calling an old stored procedure in an EntityFramework 6+ context (with transaction started), how can I get the SCOPE_IDENTITY back?

    TIA,
    Owen


    • Edited by Owen37 Friday, January 29, 2016 6:14 PM
    Friday, January 29, 2016 6:14 PM

All replies

  • if you have object b with properties id, name etc.

    you tell EF to insert this object via a procedure

    you can retrieve the id by calling b.id after you inserted this object in the database.


    Hope it helps. Spiri

    Friday, January 29, 2016 6:59 PM
  • Create new wrapper stored procedures that EF calls directly, and which return the values as EF expects.

    BTW SCOPE_IDENTITY won't give you an identity value generated in a stored procedure that you call, since you are in a different "scope".  If there are no triggers or other inserts that generate identity values then you can use @@identity instead.

    EG

    create table t(id int identity primary key, a int)
    go
    create procedure insert_t 
    as
    begin
      insert into t(a) values (1)
    end
    
    go
    go
    exec insert_t
    select scope_identity(), @@identity 

    David


    David http://blogs.msdn.com/b/dbrowne/


    Friday, January 29, 2016 7:11 PM