For. NET platform to develop WebForm projects, the programmer's method of manipulating data is mainly through the use of ado.net. And what about our MVC operations database? What is the advantage compared with ado.net?
What is the EF that everyone is talking about?
EF, full name entityframwork. is the so-called ORM (Object Relational Mapping framework, or data persistence framework) developed by Microsoft based on Ado.net.
In simple terms, it is an object-oriented framework for manipulating data in a data table based on an entity object, which is called ado.net.
Let's demonstrate how to use EF to manipulate the database:
In a database diagram, the relationships between tables are as follows:
1 How to create an Entity object Model file
① to create an Entity Data model based on Ado.net
I am here in my Models folder (can be anywhere OH) "Right" → "new Item", and then click to enter, in the template on the left, select "Data", on the right you will see "ADO." NET Entity Data model, as shown in the following illustration:
② Click Create to enter the Entity Data Model Wizard, where we can choose how to create an Entity Data model
Note: Here you can choose a model, in the bottom of the box to see the difference between the two models, there is no more explanation;
Because we created the entity model from the existing database, we chose the first "build from Database"
③ Click Next to enter the wizard to connect to the database, through a new connection, you can connect to your own SQL Server servers, then select the database we want to connect to, and generate an entity connection string, as shown in the following figure:
④ Click Next and select "Entity frame Version"
⑤ Next, select the database objects that you want to include in the model, select "Determine the single plural form of the generated objects", others remain the default, click Finish. The following figure:
A edmx file is generated underneath our models folder, and the EF Framework designer helps us generate a graph of the entity classes that are generated from the datasheet relationship, as shown in the following figure:
Note: Do you find this table somewhat similar to what we see in the database diagram? Yes, very similar, but the meaning is not the same. The database view shows the relationship between the data tables, and the EF shown here helps us to relate the entity classes generated by the data table relationship;
2 What is the edmx file generated?
The code tree for the edmx file is as follows:
How can ① be an XML file?
We open the "right" → "open mode" → in the "XML editor" to find that this EDMX file is a standard XML file, which has three major chunks, describing the relationship between our entity objects and database mappings. The following figure:
How do I generate a. cs file under the ②.tt file?
The database context class.
It is easy to find in the code tree that a. cs class file was generated below the. tt file. The code and function of the "OumindBlog.Context.cs" file are as follows:
Inherits from the DbContext class,
//Data context class, for manipulating the database. Responsible for maintaining the entity state, and generating different SQL statements to perform public
partial class Oumindblogentities:dbcontext
{public, based on the entity object wrapper class's state attribute
Oumindblogentities ()
: Base ("Name=oumindblogentities")
{
}
protected override void Onmodelcreating (Dbmodelbuilder ModelBuilder)
{
throw new unintentionalcodefirstexception ();
}
Public dbset<blogarticle> blogarticles {get; set;}
Public dbset<blogarticlecate> blogarticlecates {get; set;}
Public dbset<bloguser> blogusers {get; set;}
Public dbset<enumeration> enumerations {get; set;}
}
entity classes.
And what does another. tt file-generated class do? Let's open a look at the code:
The entity class generated by the EF entity Model Public
partial class blogarticlecate
{public
blogarticlecate ()
{this
. Blogarticles = new hashset<blogarticle> ();
}
public int Id {get; set;}
public int Author {get; set;}
public string Name {get; set;}
public string Remark {get; set;}
public int Statu {get; set;}
public bool Isdel {get; set;}
Public System.DateTime addtime {get; set;}
Generated foreign key properties public
virtual icollection<blogarticle> blogarticles {get; set;}
Public virtual Bloguser Bloguser {get; set;}
}
It is easy to find that these fields correspond to the fields in our database, which is the entity class that EF generates from the entity model and is also a foreign key attribute.
Ii. use of the EF operation database
The class that inherits DbContext's oumindblogentities is generated in the above "OumindBlog.Context.cs" to maintain the entity state and manipulate the database, so we first create the object of the class, and the code to manipulate the data is as follows:
The object that creates the database context class
Oumindblogentities db = new oumindblogentities ();
#region Query article List +actionresult Article ()
///<summary>
///query article List
///</summary>
///< returns></returns> public
ActionResult Article ()
{
//Get article List db from DB object
. Blogarticles.where (p => P.aisdel = = false);//Use LAMABDA expression to get data for deleted/
//Use LAMABDA expression to fetch
//Return a list <T> objects to store the article List
< models.blogarticle > list= db. Blogarticles.where (p => P.aisdel = = False). ToList ();
You can also use LINQ to get data
list<models.blogarticle> List1 = (from P in db. Blogarticles where P.aisdel = = False Select P). ToList ();
Use ViewData to pass the list object
viewdata["DataList" = list;
return View ();
}
#endregion
Then we create the view for article and receive the data,
Because we need to use the Blogarticle object to display the data, we should first import the namespaces
<!------------first import the namespace------------->>
@using mvcapplication1.models;
Then the code to display the data is:
copy code
<!------------get data and display HTML------------->>
<div>
<table id= " Tblist ">
<tr>
<th>id</th>
<th> title </th>
<th> Classification </th >
<th> status </th>
<th> time </th>
<th> operation </th>
</tr>
<!--traversal action method set to ViewData collection data, generate HTML code-->
@foreach (blogarticle A in viewdata["DataList"] as list& Lt blogarticle>)
{
<tr>
<td>@a.AId</td>
<td>@a.ATitle</td>
<td>@a.BlogArticleCate.Name</td>
<td>@a.Enumeration.e_cname</td>
< td>@a.aaddtime</td>
</tr>
}
</table>
</div>
The results of the operation are as follows:
Third, summary
1 The EF framework generates an Entity Data model based on the data model in our database;
2 The Entity Data Model is a EDMX file, and the file is a standard XML file, which mainly describes the entity object and the database mapping relationship;
3. tt file generates a database context class (for manipulating the database) and entity classes (representing entity objects and foreign key attribute relationships);
4 entity objects can use LAMABDA expressions or LINQ to query the required data and use a list object to store the data;
5 easy to understand the code, in the actual operation does not need to be like ado.net to create a large number of database access layer;
The above is the entire content of this article, I hope to help you learn.