ASP. net mvc Model First, mvcmodel

Source: Internet
Author: User

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=&quot;data source=.\sql2008;initial catalog=ProductDB;persist security info=True;user id=sa;password=sa123;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.