EntityFramework Executing SQL statements

Source: Internet
Author: User

Executes the SQL statement in the EF.

using(varContext =Newefrecipesentities ()) {   stringsql =@"INSERT INTO Chapter3.payment (Amount, Vendor) VALUES (@Amount, @Vendor)"; varargs =Newdbparameter[] {NewSqlParameter {parametername ="Amount", Value =99.97M},                   NewSqlParameter {parametername ="Vendor", value="Ace Plumbing"}                 }; intRowCount =context. Executestorecommand (sql, args);}
using(varContext =Newefrecipesentities ()) {   stringsql ="SELECT * from chapter3.student where degree = @Major"; varargs =Newdbparameter[] {NewSqlParameter {parametername ="Major", Value ="Masters"}}; varStudents = context. Executestorequery<student>(sql, args); Console.WriteLine ("Students ..."); foreach(varStudentinchstudents) {Console.WriteLine ("{0} {1} is working on a {2} degree", student. FirstName, student. LastName, student.  degree); }}
using(varconn =NewEntityConnection ("name=efrecipesentities")){    varcmd =Conn.    CreateCommand (); Conn.    Open (); Cmd.commandtext=@"Select C.name, c.email from Efrecipesentities.customers as C"; using(varReader =cmd. ExecuteReader (commandbehavior.sequentialaccess)) { while(reader. Read ()) {Console.WriteLine ("{0} ' s email is: {1}", reader. GetString (0), reader. GetString (1)); }    }}

1.sql = "SELECT * from Payment where vendor= @vendor"; a select * is written because the properties of the Payment object are exactly the same as the field names of the table, and if not, you need to alias the table fields. The alias needs to be the property name of the object map.

2. If the SQL statement returns fewer columns than the (materialized) entity, then EF throws an exception as it materializes, so the missing columns will need to be given meaningless values to ensure that no error is made at the exact time: eg 1, if sql= "select Paymentid , Amount from Payment "uses the context. Executestorequery<payment > (sql, args); Then the exception is reported, so the vendor column needs to be mended. The correct sql= "select Paymentid, Amount, null as Vendor from Payment".

3. If the columns returned by SQL are extra materialized for the number of entity attributes, then EF ignores the extra columns.

EntityFramework Executing SQL statements

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.