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