The name of Ef4.1,api has changed slightly, and Dbcontext.database is the encapsulation that corresponds to the database-side information. The Execute SQL command also naturally starts with the Database type. corresponding to Executestorecommand and executestorequery<t> are Database.executesqlcommand and database.sqlquery<t>.
1. Execute Command String
using (varnew Mydbcontext ()) { ctx. Database.executesqlcommand ("UPDATE person SET Name = ' Michael ' WHERE PersonID = 1") ;}
2. Execute Query String
using(varCTX =NewMydbcontext ()) { varPeopleviews = ctx. Sqlquery<personview> ("SELECT PersonID, Name from person"). ToList ();} Public classpersonview{ Public intPersonID {Get;Set; } Public stringName {Get;Set; }}
In EF4 (. NET 4), we have a brand new API:ObjectContext.ExecuteStoreCommand (...) and Objectcontext.executestorequery<t> (...). It is not difficult to know from a function name that the former is to execute a SQL command with no return set, such as a update,delete operation, which executes a query and can convert the return set to an object.
1. Execute Command String
using (varnew Myobjectcontext ()) { ctx. Executestorecommand ("UPDATE person SET Name = ' Michael ' WHERE PersonID = 1");}
2. Execute Query String
using(varCTX =NewMyobjectcontext ()) { varPeopleviews = ctx. Executestorequery<personview> ("SELECT PersonID, Name from person");} Public classpersonview{ Public intPersonID {Get;Set; } Public stringName {Get;Set; }}
Run SQL commands directly in the utility note-ef