3

I made a switch from LINQ to Entity Framework and needed to update my method's. I stumbled upon this issue where I used ExecuteCommand and my intelliSence does not reference any similar substitutes.

dc.ExecuteCommand("update Phases set PhaseID = {0} where TruckID = {1}", PhaseID, TruckID);

What can I use to accomplish this in .Net?

2 Answers2

2

You can use dc.Database.SqlQuery like this:

dc.Database.SqlQuery<YourEntity>("update Phases set PhaseID = {0} where TruckID = {1}", PhaseID, TruckID); 

I used this one in my project

Or You can use dc.Database.ExecuteSqlCommand like this:

dc.Database.ExecuteSqlCommand("update Phases set PhaseID = {0} where TruckID = {1}",PhaseID, TruckID);

second solution example here

I hope this helps to you

EDIT: The first one is meant as an actual query, that means, meant to return values, and thus will create overhead upon creating the resultSet (which is empty) and manifest it into objects. The second option is better, as it is meant as a way to do UPDATE/DELETE statements

Community
  • 1
  • 1
  • 1
    Yeah I also found the second solution on another question, I will try your first one as well. thanks. –  Jan 28 '16 at 11:53
  • The first one is meant as an actual query, that means, meant to return values, and thus will create overhead upon creating the resultSet (which is empty) and manifest it into objects. The second option is better, as it is meant as a way to do UPDATE/DELETE statements. – DevilSuichiro Jan 28 '16 at 12:07
  • Yeah you are right mate i used first one for select statement 'EntityList=uow.context.Database.SqlQuery(string.Format("select PERNR,PERIA,PEOPLE from VW_EMPLOYEES WHERE SEARCH_PEOPLE LIKE LOWER('%{0}%')", filter)).ToList();'. Anyway we learnt one more thing:) – Alper Tunga Arslan Jan 28 '16 at 12:13
1
dc.ExecuteStoreCommand("update Phases set PhaseID = "  + PhaseID  + " where TruckID = " + TruckID);

dc.ExecuteStoreCommand(string.Format("update Phases set PhaseID = {0} where TruckID = {1}"), PhaseID, TruckID);
Siva G
  • 98
  • 8