2

I have this code in .Net Framework. There is nothing wrong with it.

    // no problem in .net core
    public bool ExecuteQuery(string query, params object[] parameters)
    {
        return _context.Database.ExecuteSqlCommand(query, parameters) > 0;
    }

not found (SqlQuery) in Entity Framework Core

    public List<T> SqlQuery(string query, params object[] parameters)
    {
        var result = _context.Database.SqlQuery<T>(query, parameters).ToList();
        return result;
    }

    public object ExecuteNonQuery(string query, params object[] parameters)
    {
        var result = _context.Database.SqlQuery<string>(query, parameters);
        return result;
    }

How can I write the same code in .net core 2.1 ?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
karincayazilim
  • 123
  • 2
  • 14

1 Answers1

-1

You can't execute SqlQuery in EF Core, it requires to define a POCO class and a DbSet for that class. Then you can use it like:

using (var context = new SampleContext())
{
    var books = context.Books.FromSql("SELECT * FROM Books").ToList();
}
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48