Run SQL commands directly in the utility note-ef

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.