ASP. NET MVC Simple Introduction

Source: Internet
Author: User
Tags actionlink

Years ago I wanted to write a series of tutorials to complete ASP. NET MVC, but also help some people to avoid the pits I encountered in my work, the walls I touched. Due to limited capacity, time is not sufficient, has not been able to achieve, fortunately see Marla Sukesh wrote a 7-day tutorial, speaking very good, I would like to translate to you, in order not to deal with the hasty, I will be the original author of some practical operation to replace the individual operation, the English level is limited, please forgive me.

Rookie, please take the initiative, do not above his business, do not try to find the problem.

Great God, please leave a valuable view.


Do not hesitate to ask questions or suggestions.

Today, simply use the "Code first" model based on EF5, MVC4 and MVC scaffolding (scaffolding----and NuGet install) to create a simple book management system to warm up, the database we chose Microsoft's own SQL Server2012:

The environment is as follows: Win7 with SP1 and VS2012650) this.width=650; "title=" Image_thumb[31].png "style=" Float:none; "src="/HTTP/ S3.51cto.com/wyfs02/m00/7c/87/wkiom1bsn4_goreqaadpexijbny136.png "alt=" Wkiom1bsn4_goreqaadpexijbny136.png "/>

650) this.width=650; "title=" Image_thumb[30].png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/7C/86/ Wkiol1bsoalypnoxaakbz7sp1wm319.png "alt=" Wkiol1bsoalypnoxaakbz7sp1wm319.png "/>

650) this.width=650; "title=" Image_thumb[29].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M02/7C/86/ Wkiol1bsoahso4seaajkkisax88352.png "alt=" Wkiol1bsoahso4seaajkkisax88352.png "/>

650) this.width=650; "title=" Image_thumb[28].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M02/7C/87/ Wkiom1bsn4yc_leoaafjm4ugkcq656.png "alt=" Wkiom1bsn4yc_leoaafjm4ugkcq656.png "/>

Step 1: Create an Internet application with Razor as the engine:

650) this.width=650; "title=" Image_thumb[27].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M00/7C/87/ Wkiom1bsn4uqmz-caahjhirxskm998.png "alt=" Wkiom1bsn4uqmz-caahjhirxskm998.png "/>

650) this.width=650; "title=" Image_thumb[26].png "src=" http://s1.51cto.com/wyfs02/M00/7C/87/ Wkiom1bsoirjpdm7aagcy6qvm0w651.png "alt=" Wkiom1bsoirjpdm7aagcy6qvm0w651.png "/>

Step 2: Install EntityFramework

650) this.width=650; "title=" Image_thumb[25].png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M02/7C/87/ Wkiom1bsn4nwfmdmaajtvwuq0-g179.png "alt=" Wkiom1bsn4nwfmdmaajtvwuq0-g179.png "/>

Install EntityFramework:

Pm> Install-package EntityFramework

Installing mvcscaffolding

Pm> Install-package mvcscaffoldingattempting To resolve dependency ' t4scaffolding '. Attempting to resolve dependency ' t4scaffolding.core '. Attempting to resolve dependency ' entityframework '. Installing ' T4scaffolding.core 1.0.0 '. Successfully installed ' T4scaffolding.core 1.0.0 '. Installing ' t4scaffolding 1.0.8 '. Successfully installed ' t4scaffolding 1.0.8 '. Installing ' mvcscaffolding 1.0.9 '. Successfully installed ' mvcscaffolding 1.0.9 '.

Since it's code first, it's natural to create models:

Create three entity classes in a folder in models:

 <summary>/// Book Entity/// </summary>public class Book{     [Key]    public int ID { get; set; }     public string bookname { get; set; }    // / <summary>    /// one to one    ///  </summary>    public int publisherid { get; set; }     [foreignkey ("PublisherID")]    public virtual publisher  Publisher { get; set; }    /// <summary>     /// One to Many    /// </summary>     public virtual icollection<author> authors { get; set; }}///  <summary>/// author entity/// </summary>public class author{    [key]     public int ID { get; set; }     Public string authorname { get; set; }    public int  BookID { get; set; }}/// <summary>/// Publisher Entity///  </summary>public class publisher{    [key]    public  int ID { get; set; }    public string  publishername { get; set; }}

The MVC view---for the three entity classes is just as good as a null view.

Then open the package Manager Console and run the following command:

Pm> Scaffold Controller bookpm> Scaffold Controller authorpm> Scaffold Controller Publisher

If you want to access data through Repository then add a parameter at the end: –repository

Like:

pm> Scaffold Controller book–repository

If you want to regenerate the corresponding view and controller then add another parameter:-force

Like:

pm> Scaffold Controller Book–repository–force

Then you will get the result as follows:

650) this.width=650; "title=" Image_thumb[24].png "style=" Float:none "src=" http://s5.51cto.com/wyfs02/M01/7C/86/ Wkiol1bsn_zcilwaaasberlmt0m846.png "alt=" Wkiol1bsn_zcilwaaasberlmt0m846.png "/>

Magic Bar, automatically help you to generate a common part of the view, and database interaction with the repository, controller and the more important firstmousecontext, worry and effort. Firstmousecontext

Let's configure something to be automatically created, show it, edit shared/_layout.cshtml, add a link to it, and navigate to our view, such as:

<li> @Html. ActionLink ("Books", "Index", "Books") </li><li> @Html. ActionLink ("Authors", "Index", " Authors ") </li><li> @Html. ActionLink (" publishers "," Index "," publishers ") </li>

The following error message was obtained:

650) this.width=650; "title=" Image_thumb[34].png "style=" Float:none "src=" http://s3.51cto.com/wyfs02/M00/7C/86/ Wkiol1bsoasjgzbsaafwkj6ltcc876.png "alt=" Wkiol1bsoasjgzbsaafwkj6ltcc876.png "/>

We modify the foreign key to be empty and run again:

Pm> Scaffold Controller book-repository-forcepm> Scaffold controller author-repository-forcepm> Scaffold Cont Roller Publisher-repository–force

Run up, again wrong:

650) this.width=650; "title=" Image_thumb[36].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M01/7C/87/ Wkiom1bsn5cyacp8aafqr0w-avm872.png "alt=" Wkiom1bsn5cyacp8aafqr0w-avm872.png "/>

Obviously forgot to configure the database information, find the configuration file Web. config

<add name= "defaultconnection" connectionstring= "...." >

Modified to:

<add name= "Firstmousecontext" connectionstring= "...." >

Start again, normal:

650) this.width=650; "title=" Image_thumb[41].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M01/7C/87/ Wkiom1bsn5hqf_e4aaccq3gsqze742.png "alt=" Wkiom1bsn5hqf_e4aaccq3gsqze742.png "/>

650) this.width=650; "title=" Image_thumb[42].png "style=" Float:none "src=" http://s1.51cto.com/wyfs02/M01/7C/86/ Wkiol1bsoaxjeuk5aaca6qnlvgs717.png "alt=" Wkiol1bsoaxjeuk5aaca6qnlvgs717.png "/>

And a little bit of attention is DbContext:

Public class firstmousecontext : dbcontext{    // you can  add custom code to this file. Changes will not be   overwritten.    //    // if you want entity  framework to drop and regenerate your database    //  automatically whenever you change your model schema, add the   following    // code to the application_start method  in your Global.asax file.    // Note: this will  Destroy and re-create your database with every model  change.     //    // system.data.entity.database.setinitializer (new   system.data.entity.dropcReatedatabaseifmodelchanges<firstmouse.models.firstmousecontext> ());    public  Dbset<firstmouse.models.book> books { get; set; }    public  DbSet<FirstMouse.Models.Author> Authors { get; set; }     public DbSet<FirstMouse.Models.Publisher> Publishers { get; set;   }}

You see the explanation? Presumably, if your model schema changes, if you want to regenerate the database, you should add the following sentence to the Global.asax file:

public class mvcapplication:system.web.httpapplication{protected void Application_Start () {Arearegistrati On.        Registerallareas ();        Webapiconfig.register (globalconfiguration.configuration);        Filterconfig.registerglobalfilters (globalfilters.filters);        Routeconfig.registerroutes (routetable.routes);        Bundleconfig.registerbundles (Bundletable.bundles);        Authconfig.registerauth (); System.Data.Entity.Database.SetInitializer (New system.data.entity.dropcreatedatabaseifmodelchanges<    Firstmouse.models.firstmousecontext> ()); }}

You can actually put it in a constructor:

Public Firstmousecontext () {System.Data.Entity.Database.SetInitializer (new System.data.entity.dropcreatedatabaseifmodelchanges<firstmouse.models.firstmousecontext> ());}

Very good, very powerful, huh? Get ready for the intensive knowledge of next week's bombing ...


This article is from the "Wolf Soul" blog, please be sure to keep this source http://lybing.blog.51cto.com/3286625/1745770

ASP. NET MVC Simple Introduction

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.