In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the sample analysis of query data in ASP.NET Mvc development. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
What on earth is the EF that everyone is talking about?
EF, full name is EntityFramWork. It is the so-called ORM (object-relational mapping framework, or data persistence framework) developed by Microsoft based on ADO.NET.
To put it simply, it is an object-oriented operation framework that manipulates the data in the data table according to entity objects, and the specific underlying layer is also called ADO.NET.
Let's demonstrate how to use EF to manipulate the database:
In a database diagram, the relationship between tables is as follows:
1) how to create a solid object model file
① creates an entity data model based on ADO.NET
I right-click "→" on my Models folder (anywhere), and then click enter. In the template on the left, select "data", and you will see the option of "ADO.NET entity data Model" on the right, as shown below:
② Click to create and enter the entity data Model Wizard, where we can choose how to create the entity data model.
Note: you can select one model here. You can see the difference between the two models in the box below. There are no more explanations here.
Because we are creating an entity model from an existing database, we choose the first "generate from database"
③ clicks the next step to enter the wizard to connect to the database. By creating a new connection, you can connect to your own SQLServer server, then select the database we want to connect to, and generate an entity connection string, as shown below:
④ Click next and select "entity Framework version"
⑤ next, select the database objects that need to be included in the model, select "determine the singular and plural form of the generated objects", keep the rest by default, and click finish. As shown below:
At this point, an edmx file is generated under our Models folder, and the designer of the EF framework helps us generate a diagram of the entity classes generated according to the data table relationships, as shown below:
Note: is it found here that this table is somewhat similar to the diagram we see in the database diagram? Yes, it is very similar, but the meaning expressed is different. The database view shows the relationships between the data tables, and the EF shown here helps us generate entity classes based on the data table relationships.
2) what exactly is the generated edmx file?
The code tree for the edmx file is as follows:
How can ① be a XML file?
We right-click → to open the → in the XML editor, we can find that this edmx file is a standard XML file, which mainly has three blocks, which describe the relationship between our entity objects and database mapping. As shown below:
How to generate a .cs file under the ② .tt file?
Database context class.
We can easily find that the .cs class file is generated under the .tt file in the code tree. The code and function of the "OumindBlog.Context.cs" file are as follows:
/ / inherits from the DbContext class, / / data context class, used to operate the database. Responsible for maintaining entity state and generating different SQL statements to execute public partial class OumindBlogEntities according to the state attributes of the entity object wrapper class: DbContext {public OumindBlogEntities (): base ("name=OumindBlogEntities") {} protected override void OnModelCreating (DbModelBuilder modelBuilder) {throw new UnintentionalCodeFirstException ();} public DbSet BlogArticles {get; set;} public DbSet BlogArticleCates {get; set;} public DbSet BlogUsers {get; set } public DbSet Enumerations {get; set;}}
Entity class.
What is the use of the class generated by the other .tt file? Let's open a look at the code:
} / / generated foreign key attribute public virtual ICollection 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 generated by EF according to the entity model and becomes a foreign key attribute.
Use EF to operate the database
In "OumindBlog.Context.cs" above, we generate a class that inherits DbContext's OumindBlogEntities to maintain the entity state and manipulate the database, so we first create an object of this class, and the code to manipulate the data is as follows:
/ / create the object of the database context class OumindBlogEntities db = new OumindBlogEntities (); # region query the article list + ActionResult Article () / query the article list / public ActionResult Article () {/ / get the article list db.BlogArticles.Where through the db object (p = > p.AIsDel = = false) / / use Lamabda expressions to get deleted articles / / use Lamabda expressions to get data / / return a List object to store the article list List
< Models.BlogArticle >Then we create a view for Article and receive the data
Because we need to use BlogArticle objects to display data, we should import namespaces first
> @ using MvcApplication1.Models Then the code to display the data is: copy code > id title classification status time operation @ foreach (BlogArticle an in ViewData ["DataList"] as List) {@ a.AId @ a.ATitle @ a.BlogArticleCate.Name @ a.Enumeration.e_cname @ a.AAddtime}
The result of the operation is as follows:
III. Summary
1) the EF framework generates an entity data model based on the data model in our database
2) the entity data model is an edmx file, and the file is a standard XML file, which mainly describes the mapping relationship between entity objects and databases.
3) the .tt file generates database context classes (for manipulating the database) and entity classes (representing entity objects and foreign key attribute relationships) for us.
4) entity objects can use Lamabda expressions or Linq to query the required data, and a List object can be used to store data
5) easy to understand the code, in the actual operation, there is no need to create a large number of database access layers like ADO.net
This is the end of this article on "sample analysis of query data in ASP.NET Mvc development". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.