Db.database.ExecuteSQLCommand equivalent in EF 7

Which is equivalent

db.Database.ExecuteSqlCommand(Sql.ToString()); 

in Entity Framework 7? I saw .FromSQL () in beta 4, but haven’t seen anything like it.

+7
asp.net-core entity-framework asp.net-core-mvc entity-framework-core
source share
2 answers

Function not yet implemented. Track his progress using issue # 624 . Here is a crude extension method that you can use now.

 public static int ExecuteSqlCommand(this RelationalDatabase database, string sql) { var connection = database.Connection; var command = connection .DbConnection.CreateCommand(); command.CommandText = sql; try { connection.Open(); return command.ExecuteNonQuery(); } finally { connection.Close(); } } 

Use it as follows:

 db.Database.AsRelational().ExecuteSqlCommand("EXEC MySproc"); 

Please note that this does not take into account any active transactions.

+8
source share

Just wanted to provide an update on the latest way to use this for Entity Framework Core RC1.

In DatabaseFacade, there is an extentsion class in the Microsoft.Data.Entity namespace, which you can use as follows:

 _dbContext.Database.ExecuteSqlCommand("EXEC MySproc"); 
+6
source share

All Articles