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 ?

1

1 Answer 1

-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();
}
Sign up to request clarification or add additional context in comments.

3 Comments

This isn't the whole story any more.
@GertArnold: Care to elaborate?
There's Database.ExecuteSqlRaw now.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.