简体   繁体   English

Microsoft.EntityFrameworkCore 中 ExecuteScalar 的对应项

[英]Counterpart of ExecuteScalar in Microsoft.EntityFrameworkCore

My stored procedure has SELECT SCOPE_IDENTITY() after the insert is done.插入完成后,我的存储过程有 SELECT SCOPE_IDENTITY() 。 Using ADO.net provides ExecuteScalar to retrieve the same while calling the stored proc.使用 ADO.net 提供 ExecuteScalar 以在调用存储过程时检索相同的内容。

Is a similar feature available in Microsoft.EntityFrameworkCore(Version=3.1.5.0) which could return SCOPE_IDENTITY after sp is executed. Microsoft.EntityFrameworkCore(Version=3.1.5.0) 中是否有类似的功能,它可以在执行 sp 后返回 SCOPE_IDENTITY。 I see it has ExecuteSqlRaw but it just returns the rows affected.我看到它有 ExecuteSqlRaw 但它只返回受影响的行。

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有关更多详细信息,请参阅我的博客文章: https://erikej.github.io/efcore/2020/05/26/ef-core-fromsql-scalar.ZFC35FDC70D5FC69D269883A8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 命名空间“Microsoft.EntityFrameworkCore”中不存在“迁移” - 'Migrations' does not exist in the namespace 'Microsoft.EntityFrameworkCore' 如何禁用 Microsoft.EntityFrameworkCore 中的约定? - How to disable conventions in Microsoft.EntityFrameworkCore? 网站无法加载 Microsoft.EntityFrameworkCore - Website cannot load Microsoft.EntityFrameworkCore 索引属性解决 Microsoft.EntityFrameworkCore 中的问题 - Index Attribute resolving issue in Microsoft.EntityFrameworkCore 如何为 Microsoft.EntityFrameworkCore 3.1 方法 FirstOrDefaultAsync 创建 xunit 测试? - How to create xunit test for Microsoft.EntityFrameworkCore 3.1 method FirstOrDefaultAsync? 在.NET Standard项目中找不到Microsoft.EntityFrameworkCore类型 - Type not found Microsoft.EntityFrameworkCore in .NET Standard project 安装Microsoft.EntityFrameworkCore v1.1.4时出错 - Error Installing Microsoft.EntityFrameworkCore v1.1.4 Microsoft.EntityFrameworkCore:没有为此 DbContext 配置数据库提供程序 - Microsoft.EntityFrameworkCore: No database provider has been configured for this DbContext 无法从 Nuget 包管理器安装 Microsoft.EntityFrameworkCore - Not able to install Microsoft.EntityFrameworkCore from Nuget Package Manager Microsoft.EntityFrameworkCore ProjectTo() 找不到我的存储库的方法定义 - Microsoft.EntityFrameworkCore ProjectTo() Method definition not found for my repository
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM