Query data for ASP. NET Mvc development, asp. netmvc
To develop a WebForm project on the. NET platform, programmers can use ADO. NET to operate data. What about the MVC database? What are the advantages compared with ADO. NET?
I. What is EF?
EF, full name: EntityFramWork. It is the so-called ORM (Object link ing framework, or data persistence framework) developed by Microsoft Based on ADO. NET ).
Simply put, it is an object-oriented operation framework that operates data in a data table based on an object. The underlying layer also calls ADO. NET.
Here we will demonstrate how to use EF to operate databases:
In the database graph, the relationship between tables is as follows:
1) how to create an object model file
① Create an object data model based on ADO. NET
Click "right-click"> "new item" in my Models folder (which can be anywhere), and select "data" in the template on the left ", the "ADO. NET Object Data Model option, such:
② Click Create to go to the "Object Data Model Wizard". Here we can choose how to create an object data model.
Note: here you can select a model. You can see the differences between the two models in the box below. I will not explain them here;
Because we create an entity model from an existing database, select the first "generate from database"
③ Click Next to enter the Database Connection Wizard. By creating a new connection, you can connect to your own SQLServer server, select the database to be connected, and generate a physical connection string, for example:
④ Click Next and select "Entity Framework Version"
⑤ Next, select the database objects to be included in the model, and select "confirm the singular and plural forms of the generated objects". Keep others by default. Click Finish. For example:
At this time, an edmx file will be generated under our Models folder. At the same time, the EF framework designer will help us generate an object class relationship diagram based on the data table relationship, such:
Note: Is this table similar to the graph we see in the database graph? Yes, it is similar, but the meaning is different. The database view shows the relationship between data tables. The EF shown here helps us to generate the relationship between object classes based on the relationship between data tables;
2) What is the generated edmx file?
The code tree of the edmx file is as follows:
① How can it be an XML file?
By right-clicking → opening method → opening in the XML editor, we can find that this edmx file is a standard XML file, which consists of three main parts, it describes the ing between our object and the database. For example:
② How does one generate the. cs file under the. tt file?
Database context class.
In the code tree, we can easily find that the. cs class file is generated under the. tt file ." The code and functions of the OumindBlog. Context. cs file are as follows:
// Inherits from the DbContext class and // data context class for database operations. Maintains the entity state and generates different SQL statements based on the state attributes of the entity object packaging class. Execute public partial class OumindBlogEntities: DbContext {public OumindBlogEntities (): base ("name = OumindBlogEntities ") {} protected override void OnModelCreating (DbModelBuilder modelBuilder) {throw new topology ();} 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 class.
What is the use of the Class generated by another. tt file? Let's open a code:
// EF object class 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;} // The generated foreign key attribute public virtual ICollection <BlogArticle> BlogArticles {get; set;} public virtual BlogUser {get; set ;}}
It is easy to find that these fields correspond to the fields in our database. This is the entity class generated by EF according to the entity model and is still a foreign key attribute.
Ii. Use EF to operate databases
In the above "OumindBlog. context. cs "generates the OumindBlogEntities class that inherits DbContext to maintain the object status and operate the database. Therefore, we must first create an object of this class and the Code for operating the data is as follows:
// Create the database context Class Object OumindBlogEntities db = new OumindBlogEntities (); # region Query Article list + ActionResult Article () /// <summary> /// query the document list /// </summary> /// <returns> </returns> public ActionResult Article () {// obtain the list of articles db through the db object. blogArticles. where (p => p. AIsDel = false ); // use the Lamabda expression to obtain the deleted article // use the Lamabda expression to obtain data // return a List <T> object to store the article List <Models. blogArticle> list = db. blogArticles. where (p => p. AIsDel = false ). toList (); // you can also use Linq to obtain the 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
Next, we create a view for Article and receive data,
Because we need to use the BlogArticle object to display data, we should first import the namespace
<! ------------ First import the namespace -------------> @ using MvcApplication1.Models; then, the code for displaying the data is: copy the Code <! ------------ Get data and display html ------------- >>< div> <table id = "tbList"> <tr> <th> id </th> <th> title </th> <th> Category </th> <th> Status </th> <th> time </th> <th> operation </th> </tr> <! -- Traverse the set data set to ViewData by the Action method and generate the HTML code --> @ foreach (BlogArticle a in ViewData ["DataList"] as List <BlogArticle>) {<tr> <td> @. AId </td> <td> @. ATitle </td> <td> @. blogArticleCate. name </td> <td> @. enumeration. e_cname </td> <td> @. AAddtime </td> </tr >}</table> </div>
The running result is as follows:
Iii. Summary
1) The EF framework generates an Entity Data Model Based on the Data Model in our database;
2) The object data model is an edmx file and a standard XML file. It mainly describes the ing between object and database;
3) The tt file generates the database context class (used to operate the database) and object class (representing the relationship between object and foreign key attributes) for us );
4) the object can use the Lamabda expression or Linq to query the required data and use a List object to store the data;
5) easy to understand the code. In actual operations, you do not need to create a large number of database access layers like ADO.net;
The above is all the content of this article, hoping to help you learn.
Articles you may be interested in:
- ASP. NET MVC5 website development framework model, data storage, and business logic (3)
- ASP. NET MVC5 website development user logon and cancellation (5)
- ASP. NET MVC5 website development users modify data and passwords (6)
- Add an article for ASP. NET MVC5 website development (8)
- ASP. NET MVC5 website development Display Article list (9)
- ASP. NET MVC5 website development and modification and deletion of articles (10)
- ASP. NET MVC5 website development management list, reply and delete (13th)
- Development of ASP. NET Mvc with EF delayed Loading