If i have a table as called "users" and i want give only a query for example to show user as have id > 1.
How to do that in ASP.NET Core.
in php you can do like that:
$sql = "SELECT * FROM users WHERE id='1';";
but how to do same with ASP.NET CORE.
Sorry for my bad English.
1 Answer
You can use the FromSqlRaw extension method to begin a LINQ query based on a raw SQL query. FromSqlRaw can only be used on query roots, that is directly on the DbSet<>.
Note:DbSet.FromSql prior to Entity Framework Core 3.0
For parameterized queries , you could refer to the below examples:
1)Format string
var author = db.Authors.FromSqlRaw("SELECT * From Authors Where AuthorId = {0}", id).FirstOrDefault();
2)Create DbParameter objects for the provider that you are using.
var p1 = new SqliteParameter("@Id", id); // parameter construction for SqLite
var author = db.Authors.FromSqlRaw($"SELECT * From Authors Where AuthorId = @Id", p1).FirstOrDefault();
Reference:
1 Comment
Fadi
I will try it and come back. Thanks