The EF Framework's operations on the database

Source: Internet
Author: User

Web-based database operations:

Traditional method: ADO

Query, modify, delete, and insert new records by calling SQL statements from an ADO object.

New method: EF

Converts a traditional database into an object-oriented entity (entity) and becomes the definition of a class (database counterpart, traditional table Table object)

Microsoft offers: EntityFramework for short, EF

Entity Framework

Benefits of EF:

Avoid mixing SQL syntax with C # syntax;

All use visual object-oriented syntax.

There are real-time code hints.

Two ways to model database:

Db-first a database first, then a model

(generally using Db-first)

Code-first is modeled first, and then the database is generated according to the model.

Xsdbentities mydb = new Xsdbentities ();

Generates an object for the entity class MyDB

Xsbasetable Myrecord = Mydb.xsBASETABLE.First (); Find the first record

Generate a record of the student base table corresponding to the record to be deleted

Mydb.xsBASETABLE.Remove (Myrecord); Delete a specified record in a model

MyDB. SaveChanges ();

Saves the operation of the model to SQL Server.

protected void Button1_Click (object sender, EventArgs e)

{

Add a record

Xsdbentities mydb = new Xsdbentities ();

Xsbasetable Myrecord = new Xsbasetable () {xh= "004", Name= "dddd", address= "one 101"};

Create a record of a new table and assign a value directly after the constructor

MYDB.XSBASETABLE.ADD (Myrecord);

To add a new record to the model

MyDB. SaveChanges ();

Saves the operation of the model to SQL Server.

}

protected void Button3_Click (object sender, EventArgs e)

{

Modify a record

Xsdbentities mydb = new Xsdbentities (); New Entity Object

Xsbasetable Myrecord = Mydb.xsBASETABLE.First (); Find the first record

Myrecord.xh = "008"; Modify each field of the record

Myrecord.name = "Liming";

myrecord.address = "Four building 201";

MyDB. SaveChanges ();

Saves the operation of the model to SQL Server.

}

protected void Button4_Click (object sender, EventArgs e)

{//Query code

Xsdbentities mydb = new Xsdbentities (); Generating Entity objects

Xsbasetable Myrecord = Mydb.xsBASETABLE.First (); Find the records you need

This.  TextBox1.Text = Myrecord.name; Assign a field of this record to a text box

}

protected void Button4_Click (object sender, EventArgs e)

{

Code that queries any condition

Xsdbentities mydb = new Xsdbentities (); Generating Entity objects

var Myrecord = mydb.xsbasetable.

Where (c = c.xh = = "005" && c.name= "dddd");

Find a collection of records that meet the criteria

var stands for any type of variable

This. TextBox1.Text = Myrecord. FirstOrDefault (). Name;

Assigns a field from the first record in a record collection that satisfies a condition to a text box

}

Multiple record deletions:

Xsdbentities mydb = new Xsdbentities ();

VAR[U1] myrecord[u2] = Mydb.xsBASETABLE.Where (N[u3] =>[u4] n.xh==[u5] "004"); Find a collection that satisfies a condition record

foreach (var temp in Myrecord)//iterate over each object in the collection

{

Mydb.xsBASETABLE.Remove (temp); Delete a specified record in a model

}

MyDB. SaveChanges ();

Saves the operation of the model to SQL Server.

[U1] represents any data type.

The Var a=3 computer knows that a is an integral type.

[U2] Variable collection

[U3] Any variable name

[U4] Lambda expression

[U5] is a C language sentence.

The EF Framework's operations on the database

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.