简体   繁体   中英

Counterpart of ExecuteScalar in Microsoft.EntityFrameworkCore

My stored procedure has SELECT SCOPE_IDENTITY() after the insert is done. Using ADO.net provides ExecuteScalar to retrieve the same while calling the stored proc.

Is a similar feature available in Microsoft.EntityFrameworkCore(Version=3.1.5.0) which could return SCOPE_IDENTITY after sp is executed. I see it has ExecuteSqlRaw but it just returns the rows affected.

You can do this:

using (var db = new NorthwindContext())
{
    var result = db.Set<IntReturn>()
    .FromSqlRaw("exec dbo.Scalar")
    .AsEnumerable()
    .First().Value;
    
    Console.WriteLine(result);
}

See my blog post here for more details: https://erikej.github.io/efcore/2020/05/26/ef-core-fromsql-scalar.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM