ADO Framework,cold First Simple example

Source: Internet
Author: User

First, install entityframework;

Open NuGet in VS, enter: Install-package entityframework on the command line.

Ii. Basic methods of use

1. Building a data Model

    class Student    {        publicintgetset;} // ID         Public string Get set; }// name public        bytegetset;} // Age    }

2. Establishing the data context

using System.Data.Entity; // referencing the entity Framework namespace sql{    class  studentcontext:dbcontext    {        publicbase( @" Data source=mypc\mssqlserver;initial Catalog=entitycodefirst; User Id=sa; password=123456") {}        publicgetset;}}    }

3. Use

usingSystem;namespacesql{classProgram {Static voidMain (string[] args) {            //Save            using(varData=NewStudentcontext ()) {                varStudent1 = data. Students.create ();//Create a student's objectStudent1.name ="Little Mann"; Student1.age= the; Data. Students.add (student1);//adding student objects to student tables                varStudent2 =data.                Students.create (); Student2.name="Little Red"; Student2.age= the; Data.                Students.add (Student2); Data. SaveChanges ();//Keep it in the database.            }            //Read            using(vardata =NewStudentcontext ()) {                foreach(varIteminchdata. Students) {Console.WriteLine ("id:{0}, Name: {1}, Age: {2}", Item.id,item.name,item.age); }} console.readkey (true); }    }}

ADO Framework,cold First Simple example

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.