This question already has answers here:
Raw SQL Query without DbSet - Entity Framework Core (23 answers)
Closed7 years ago.
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 ?
- 1learn.microsoft.com/en-us/ef/core/modeling/query-typesGert Arnold– Gert Arnold2018-08-03 20:04:32 +00:00CommentedAug 3, 2018 at 20:04
1 Answer1
You can't execute SqlQuery in EF Core, it requires to define a POCO class and aDbSet 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
Gert Arnold
This isn't the whole story any more.
Trevortni
@GertArnold: Care to elaborate?
Gert Arnold
There's
Database.ExecuteSqlRaw now.Explore related questions
See similar questions with these tags.