Continue to the previous section of the initial ASP. NET MVC4, continue in-depth study, feel the Microsoft MVC4+EF5 (ENTITYFRAMEWORK5) framework of the powerful, can efficiently develop the Web application development system, see how to use MVC4+EF5 to achieve data reading.
Achieve Results
Operation Steps1. Create a database
--Create TABLE--create table News (ID int identity (primary) key, Newstitle varchar () NOT NULL, Createtime Datetime, Context Text, Author varchar (10))--insert data--insert into dbo. News (Newstitle,createtime,context,author) VALUES (' Learning Mvc4 ', GETDATE (), ' We come together to play MVC4 ', ' ding Xiao ') insert INTO dbo. News (Newstitle,createtime,context,author) VALUES (' I will Unity ', GETDATE (), ' cross-platform unity is good ', ' ding Xiao ')
Create the database and add two data to make it easier for us to call later.
2. Create a model entity object
In the model file, right-click Add-New Project--data->ado. NET Entity Data model, rename: Mvcdemomodel, click Generate.
Connecting to a local database
Select a database connection
Fill in the connection information
Select a table
Click Generate, you will find that the models folder generated the corresponding model entity object models, this is the entityframework of the strong point.
The following is a News.cs file that opens and discovers the entity object model
The Mvcdemoentities class in the Mvcdemomodel file is the class that operates the entity object model to be displayed
3. Send the data in the database to the front-end display in the controllerOpen the Controller file and add the following code to read the data and compile it once
Public ActionResult Index () { models.mvcdemoentities db = new models.mvcdemoentities (); List<models.news> News = db. News.where (U = u.id>0). ToList (); Return View (News);
after the compilation succeeds, continue to the previous section of the Add View
Then found in the Views folder automatically generated the corresponding controller view file, we open will find a page generated, running effect as shown above.
@model ienumerable<mvcapplication1.models.news>@{viewbag.title = "Index";}
The above is the simplest example of MVC+EF data read, Microsoft has done a lot of work for us, but this automatic word generation operation is not suitable for our novice learning, we still have to manually create a blank file to achieve the effect of this display, but also know that Microsoft does these operations of the principle of things.
4. Write your own page to show the effect you want to show@model ienumerable<mvcapplication1.models.news> //Make a reference to a header file @{ viewbag.title = "Index";}
This is similar to the previous ASP era, and the use of the template engine under the current. NET era is much more like that!
5. Add a style sheetThe above table has no style is really too bad to see, simply add a style sheet, and then run to see the effect
<style type= "Text/css" > #tb { } #tb tr td{ border:1px solid #090808; } </style>
6.Linq Operation ModelLambda can manipulate model,linq also can query return data, these two effects are equal.
LINQ mode
var news = from N in db. News Select N;
Lambda mode
List<models.news> News = db. News.where (U = u.id>0). ToList ();
The result of the operation is the same, before the three-layer write more complex, now out of MVC is much more convenient.
Welcome to watch my scarf.
==================== csdn Blog Column =================
myblog:http://blog.csdn.net/dingxiaowei2013 myqq:1213250243
Unity QQ Group:375151422 cocos2dx QQ Group: 280818155
====================== learn from each other and progress together ===================