ASP. net mvc Model First, mvcmodel
I just learned MVC and I don't know where to learn it. Simply start learning from Model First. After all, some people say that Code First is too difficult to maintain.
First, add a new project. Select ADO. NET Object Data Model
At this time, we will add a file suffixed with. edmx in our project. Next, we should design the database table as OK, that is, the database table relationship diagram I designed. After the design, we use
Right-click to generate a database based on the model.After the database is generated, we usually see the connection string of the relevant database in the Web. Config configuration file (I am here EFDbContextContainer)
<connectionStrings> <add name="EFDbContextContainer" connectionString="metadata=res://*/Models.EFDbContext.csdl|res://*/Models.EFDbContext.ssdl|res://*/Models.EFDbContext.msl;provider=System.Data.SqlClient;provider connection string="data source=.\sql2008;initial catalog=ProductDB;persist security info=True;user id=sa;password=sa123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
Now the basic work has been completed, and the next step is coding.
I created a HomeController In the Controller and a Home directory under Views, and put an Index page under the Home directory. This page is the data page we want to display.
Namespace ModelFirstDemo. controllers {public class HomeController: Controller {EFDbContextContainer context = new EFDbContextContainer (); // instantiate the data access context public ActionResult Index () {var products = context. productSet. toList (); // obtain all product data through data access context return View (products );}}}
Code for displaying page data:
@ Model IEnumerable <ModelFirstDemo. models. product >@{ ViewBag. title = "Product ";} <table cellpadding = "0" cellspacing = "0" border = "1" width = "50%"> <tr> <th> NO. </th> <th> product name </th> <th> unit price </th> <th> Inventory </th> </tr> @ foreach (var product in Model) {<tr> <td align = "center"> @ product. proudctID </td> <td align = "center"> @ product. productName </td> <td align = "center"> @ product. unitPrice </td> <td align = "center"> @ product. stockCount </td> </tr >}</table>
Well, this is the end of a basic article.