简体   繁体   中英

postgreSQL returning Id's of updated rows using entity framework

I am trying to return the ID's of the rows that were affected by an update. I am using entity frameworks ExecuteSqlCommand and postgreSQL database. When I execute the command all I get as a return value is -1. What am I doing wrong?

var updatedIds = _dbContext.Database.ExecuteSqlCommand(@"UPDATE dbo.""PrincipleCategoryObjectives"" SET ""IsRequired""=" + requiredDto.IsRequired + @" WHERE ""AuditPrincipleId""=" + requiredDto.PrincipleId + @" RETURNING ""Id""");

ExecuteSqlCommand returns the number of rows affected, and not any value returned by the query itself ( see the API docs ).

To get a value returned from your SQL, you can drop down to ADO.NET. Call _dbContext.Database.GetDbConnection() to get a plain old DbConnection, create a DbCommand on that connection with your SQL and call ExecuteScalar() on that (see the many tutorials on the web if you need help).

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