Last post Sep 09, 2016 02:21 PM by Siva Krishna Macha
Sep 09, 2016 01:49 PM|sudip_inn|LINK
just see two set of code and tell me what is difference between SqlQuery and SqlCommand in EF
var allUsers = db.Database.SqlQuery<SqlUserAndRoleRow>(
@"select mp.name as UserName, rp.name as RoleName, mp.type as UserType
from sys.database_role_members drm
join sys.database_principals rp on (drm.role_principal_id = rp.principal_id)
join sys.database_principals mp on (drm.member_principal_id = mp.principal_id)
ORDER BY UserName");
using (var context = new BloggingContext())
"UPDATE dbo.Blogs SET Name = 'Another Name' WHERE BlogId = 1");
Sep 09, 2016 02:20 PM|Rion Williams|LINK
As your examples indicate, both of these allow you to use Entity Framework to execute raw SQL queries. Generally, the SqlQuery method is used for actual queries where you expect data to be returned (i.e. SELECT statements) and SqlCommands are going to be
more commonly used for non-queries (i.e. UPDATE, DELETE, etc.).
As such, the ExecuteSqlCommand() method will return the number of rows affected by the query, whereas SqlQuery returns a collection of entities or objects for the given query.
Sep 09, 2016 02:21 PM|Siva Krishna Macha|LINK
Generally, SqlQuery is used for select operations.
SqlCommand is used for Update, Delete and Insert operations. However, I believe, it is ExecuteSqlCommand but not just SqlCommand.