The development environment for VS Ultimate, first create a new MVC project called Guestbook, the specific process is not table, because I am more lazy haha!
One, the default template analysis (the development tool will automatically create a project that can run):
1. Controller: A class that inherits from the Controller base class, where a method (called an action) corresponds to a URL.
1 namespaceguestbook.controllers2 {3 Public classHomecontroller:controller//inherit from controller4 {5 Publicactionresult Index ()6 {7 returnView ();8 }9 Ten PublicActionResult About () One { A //ViewBag is essentially a dictionary, - //it provides a way to store the data that can then be accessed in the view -Viewbag.message ="Your Application description page."; the - returnView ();//indicates which view should be rendered - } - + Publicactionresult Contact () - { +Viewbag.message ="Your Contact page."; A at returnView (); - } - } -}
View Code
2. Registering a route: The RegisterRoutes method defines a route that maps the URL pattern to a controller or an action.
1 namespaceGuestbook2 {3 Public classRouteconfig4 {5 Public Static voidregisterroutes (routecollection routes)6 {7Routes. Ignoreroute ("{Resource}.axd/{*pathinfo}");//The framework does not have to process any path that contains the. axd extension file name8 9 routes. MapRoute (TenName"Default",//Route name OneUrl:"{Controller}/{action}/{id}",//URL pattern ADefaultsNew{controller ="Home", action ="Index", id = urlparameter.optional}//route Default Value - ); - } the } -}
View Code
Second, Code first way to create a SQL Server database:
1. Add a model: the corresponding data table in the database
1 namespaceGuestbook.models2 {3 Public classGuestbookentry4 {5 Public intId {Get;Set; }6 Public stringName {Get;Set; }7 Public stringMessage {Get;Set; }8 PublicDateTime dateadded {Get;Set; }9 }Ten}
View Code
2. Use the ORM Tool--entity the framework to interact with the database:
1). Add a class that inherits from the DbContext base class:
1 namespaceGuestbook.models2 {3 Public classGuestbookcontext:dbcontext4 {5 PublicGuestbookcontext ()6 { 7 }8 9 PublicDbset<guestbookentry> Entries {Get;Set; }Ten } One}
View Code
The context class also defines a separate property, entries, as a collection of query data in the Guestbookentries table (the complex number that EF automatically creates as the entity model name).
2). Add the database connection string in the Web. config file:
<name= "Guestbookcontext" connectionString= "Data source=.;i Nitial catalog=guestbook;integrated security=true " providerName=" System.Data.SqlClient " />
View Code
Note: 1. The name of the connection string should be the same as the name of the context class, or the name of the string will be passed to the constructor of the context class;
2. Running the program at this time does not create a database! Because it was a bit of a long time, it was later discovered that the database will be automatically created when the action is taken on the data table. Hey, rookie is a rookie ...
Three, pearl milk tea (increase and revise)--operating data:
1. The options in the selection box automatically create CRUD action methods and associated View files:
2. Select the model and context class:
3. At this point, run the program input URL as follows:
4. Display the list of data in the Guestbookentries table because no data has been added so the list is empty and there is only one create new link; but now the database and the corresponding table should be created, such as:
5. Click the Create new link, jump to the new page, fill in the corresponding content after the submission:
After the successful submission, re-jump to the list page, you can see each column of data corresponding to the edit and delete operations, is not very powerful!
The next chapter tries to analyze the automatically generated code, to be continued ...
"NET MVC 4 Combat" study Note 1: First MVC program (top)