Last post Sep 27, 2018 08:45 AM by Zhi Lv - MSFT
Sep 07, 2018 02:45 PM|Taurus_amat|LINK
Sep 07, 2018 04:10 PM|PatriceSc|LINK
DbContext.Database.SqlQuery is to allow the use of EF on non entity queries. If you want to use DataTable just use ADO.NET.
It's best to be explicit about the error you have but miore likely the issue is that rather than filling a DataSet DbContext.Database.SqlQuery try to map column names to properties of the DataTable object.
Are you really sur you don't want to use EF or some other ORM?
Sep 07, 2018 06:01 PM|DA924|LINK
How can i execute a query and get data using in mvc
MVC has nothing to do with it. ADO.NET, SQL Command Objects, inline T-SQL and a datareader can be used at anytime without an ORM even being involved and the DTO pattern
Sep 27, 2018 08:45 AM|Zhi Lv - MSFT|LINK
You could refer to my reply in
this thread, and refer to the following code to execute sql command.
using (DBEntities dBEntities = new DBEntities())
dBEntities.Database.ExecuteSqlCommand("insert into Table1 values(1088, 'Conditioner', 'expense4',590);");
var query = dBEntities.Table1.SqlQuery("select * from Table1").ToList<Table1>();
var query2 = dBEntities.Database.SqlQuery<Table1>("select * from Table1").ToList<Table1>();
var query3 = dBEntities.Database.SqlQuery<tablevm>("select t1.id, t1.product, SUM(t1.price) as price from Table1 as t1 group by t1.id, t1.product").ToList<tablevm>();