Entity Framework 7 Return value of FromSql stored procedure

I am trying to return a value using the new FromSql in the Entity Framework 7. My stored procedure returns 0 if everything goes well, and 1 if an error occurs.

Using FromSql with DbSet , we can, for example, do

 _dbContext.ExampleEntity.FromSql('someSproc', 'param') 

How do you get a scalar return value of 0 or 1 from this?

+2
source share
1 answer

It seems that support for stored procedures is still lagging .

Here is an example extension method that you can call with _dbContext.ExecuteStoredProcedure("someSproc", "param"); .

 public static class DbContextExtension { public static int ExecuteStoredProcedure(this DbContext context, string name, string parameter) { var command = context.Database.GetDbConnection().CreateCommand(); command.CommandType = CommandType.StoredProcedure; command.CommandText = name; var param = command.CreateParameter(); param.ParameterName = "@p0"; param.Value = parameter; command.Parameters.Add(param); return (int)command.ExecuteScalar(); } } 
+3
source

Source: https://habr.com/ru/post/1238474/


All Articles