Continue with the initial ASP. NET MVC4, continue to study in depth, feel the strength of Microsoft's MVC4 + EF5 (EntityFramework5) framework, and be able to efficiently develop a website application development system, the following describes how to use MVC4 + EF5 to read data.
Effect
Step 1. Create a database
-- Create table News (id int identity (1,1) primary key, NewsTitle varchar (100) not null, CreateTime Datetime, Context text, Author varchar (10 )) -- insert data -- insert into dbo. news (NewsTitle, CreateTime, Context, Author) values ('learn mvc4 ', GETDATE (), 'Let's play MVC4 together, 'ding Xiaowei') insert into dbo. news (NewsTitle, CreateTime, Context, Author) values ('I will generation', GETDATE (), 'cross-platform unity is also good', 'ding Xiaowei ')
After the database is created, add two pieces of data to facilitate subsequent calls.
2. Create a Model object
Right-click the model file and choose "add"> "new project"> "data"> "ADO. NET". Rename the model: MVCDemoModel and click "generate.
Connect to local database
Select database connection
Enter connection information
Select a table
Click Generate to generate the corresponding Model object Model under the Models folder. This is the power of EntityFramework.
The following is a news. cs file. Opening it will show that it is an object model.
The MVCDemoEntities class in the MVCDemoModel file is the class for operating the object model to be displayed.
3. Get the data in the database in the Controller and send it to the front-end. Open the Controller file, add the following code to read the data, and compile it once.
public ActionResult Index(){ Models.MVCDemoEntities db = new Models.MVCDemoEntities(); List
news = db.News.Where(u => u.id>0).ToList(); return View(news);}
After compilation is successful, continue adding the view in the previous section.
Then we found that the corresponding controller View File is automatically generated under the Views folder. When we open it, we will find that a page is generated, and the running effect is shown above.
@model IEnumerable
@{ ViewBag.Title = "Index";}Index
@Html.ActionLink("Create New", "Create")
@Html.DisplayNameFor(model => model.NewsTitle) |
@Html.DisplayNameFor(model => model.CreateTime) |
@Html.DisplayNameFor(model => model.Context) |
@Html.DisplayNameFor(model => model.Author) |
|
@foreach (var item in Model) {
@Html.DisplayFor(modelItem => item.NewsTitle) |
@Html.DisplayFor(modelItem => item.CreateTime) |
@Html.DisplayFor(modelItem => item.Context) |
@Html.DisplayFor(modelItem => item.Author) |
@Html.ActionLink("Edit", "Edit", new { id=item.id }) | @Html.ActionLink("Details", "Details", new { id=item.id }) | @Html.ActionLink("Delete", "Delete", new { id=item.id }) |
}
The above is the simplest example of reading MVC + EF data. Microsoft has done a lot of work for us, but this automatic generation operation is not suitable for new users, we still need to manually create a blank file to achieve the effect of this presentation. We also know how Microsoft works with these operations.
4. Write a page by yourself to show what you want to display
@ Model IEnumerable
// Reference a header file @ {ViewBag. Title = "Index";} to display the list
Title |
Release Date |
Publisher |
@ Foreach (var item in Model ){
@ Item. NewsTitle |
@ Item. CreateTime |
@ Item. Author |
}
This method is similar to the asp era and the template engine in the. net era!
Vcq11NrKx8yrsru6w7 + 0wcujrLzytaXM7bzTuPbR + assets/tNCnufs8L3A + PHA + PC9wPjxwcmUgY2xhc3M9 "brush: java;">
6. Create a Model for the Linq operation.
Lambda can operate on models, while Linq can also query returned data. The two effects are the same.
// Use the Linq Method
Var news = from n in db. News select n;
// Lambda Method
List News = db. News. Where (u => u. id> 0). ToList ();
The running results are the same. The previous three-layer writing is complicated. Now MVC is much more convenient.
Welcome to my bib
================================== Publisher Ding Xiaowei CSDN blog column ================ =====
MyBlog: http://blog.csdn.net/dingxiaowei2013 MyQQ: 1213250243
Unity QQ group: 375151422 cocos2dx QQ group: 280818155
==================================== Mutual learning, common progress ==============================