Learning ASP. NET MVC also has a period of time, intends to get a small program to practice practiced hand, as the learning process of recording and sharing.
First of all, need to determine demand, wood needs, it also make a yarn! Well...... Think about it for a moment and finally come to the following requirements:
1, can be customized classification
2, can send the article
Ok! In this way, start from the simple (in fact, the complex will not do), the need to add later (Hope level can be achieved). function is determined, then change to determine what to look like. First discuss with the degree Niang first ...
Finally in my mercy, degrees Niang hand over a relatively concise, the source of the site here (said thanks), by the younger brother castrated after the effect such as:
Did you start coding then? Well! Let's go! Open your beloved VS2013, build a Web project, use the. NET framework4.5.1 framework, this is the latest I can use, why use the latest? Because...... Like, that's enough! (Personal practice, it doesn't matter what it is!!!) )。 Solution name is Showpin project name is Showpin.web
Another one, choose the MVC template, the others have not chosen, will not use =_=!! Authentication with a personal user account, this is to lazy, saying that the net identity is still very strong, do not have to white ^_^ ~
After clicking OK, you will see the following interface:
Solution structure:
First to update a first:
All right, let's get started!! Create a category and blog (POST) Two classes under the models directory, with the code posted below
Category:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespaceshowpin.web.models{/// <summary> ///category/// </summary> Public classCategory { PublicCategory () { This. Posts =NewList<post>(); } /// <summary> ///get or set category headings/// </summary> Public stringTitle {Get;Set; } /// <summary> ///The content collection under this category/// </summary> Public VirtualIcollection<post> Posts {Get;Set; } }}
Post:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;namespaceshowpin.web.models{/// <summary> ///Blog/// </summary> Public classPost {/// <summary> ///Gets or sets the content title/// </summary> Public stringTitle {Get;Set; } /// <summary> ///Gets or sets the content/// </summary> Public stringContent {Get;Set; } /// <summary> ///Gets or sets the content release date/// </summary> PublicDateTime CreateDate {Get;Set; } /// <summary> ///Gets or sets the number of hits/// </summary> Public intHits {Get;Set; } /// <summary> ///Gets or sets the category ID/// </summary> Public stringCategoryId {Get;Set; } /// <summary> ///Gets or sets the classification/// </summary> Public VirtualCategory Category {Get;Set; } }}
Well, then there's the legendary DbContext.
ObjectContext:
usingSystem;usingSystem.Collections.Generic;usingSystem.Data.Entity;usingSystem.Linq;usingsystem.web;namespaceshowpin.web.models{/// <summary> ///Data Context/// </summary> Public classObjectcontext:dbcontext { PublicObjectContext ():Base("Showpincontext") { } /// <summary> ///Category List/// </summary> PublicDbset<category> Categories {Get;Set; } /// <summary> ///List of posts/// </summary> PublicDbset<post> Posts {Get;Set; } }}
Add the following code to the ConnectionStrings node of the Web. config:
<add name="showpincontext" connectionstring="Data source= (LocalDb) \ V11.0;initial catalog=aspnet-showpin.context;integrated Security=sspi; attachdbfilename=| Datadirectory|\aspnet-showpin.context.mdf" providername="System.Data.SqlClient " />
It's like this, look at the picture below.
Well, then let's use the database migration feature to get the code to behave in the database.
Here's the package management console.
Input: Enable-migrations then press the "enter" key
This one...... Something went wrong, alas ... On the wrong foot!! The reason for the error is that there are two context types in the current project, and SB VS does not know which one to use to build the database, so ...
Input: Enable-migrations-contexttypename ShowPin.Web.Models.ObjectContext Again "enter"
It's another mistake, =_=!!.
One or more validation errors were detected during model generation:
ShowPin.Web.Models.Category:: EntityType "Category" does not have a key defined. Please define the key for the EntityType.
ShowPin.Web.Models.Post:: EntityType "Post" does not have a key defined. Please define the key for the EntityType.
Categories:EntityType:EntitySet "Categories" is based on the type "category" for which no key is defined.
Posts:EntityType:EntitySet "Posts" is based on type "Post" with no key defined.
This is because there is no primary key defined, in simple terms is the legendary ID ... Adding a primary key to an entity class
/// <summary> /// PRIMARY Key /// </summary> Public string Get set; }
The string type of the primary key, or is relatively rare.
One more time, enter: Enable-migrations-contexttypename ShowPin.Web.Models.ObjectContext again "enter"
I'm a monkey, are you kidding me?
Let's do it again!!
Input: Enable-migrations-contexttypename showpin.web.models.objectcontext-force Again "enter"
It's done, hahaha, haha ...
He also generated some files to see what it was.
More than one migrations directory, and Configuration.cs file, what is this thing? No matter what he is.
The next step is to do a CRUD operation on the Category and create a new Controller
After clicking OK, the miracle appears T-T, in the browser input: http://localhost:10223/Category
The address is my local address, please change it according to the actual situation.
Very sorry, wrong!!!
Because after the database migration was enabled and not updated, let's update it!! Open the Package Manager console and enter add-migration ini "enter"
There are a few more files in the Migrations directory.
What the hell is that? Ignore it first, then enter: Update-database
Looks like a success, look at the App_Data catalogue
The database is built, and then the page is refreshed
Finally succeeded, haha ... And then the Post.
Done!
Here's a little summary:
1, the above appear some small problems, basically is due to not carefully caused, so in the next study must be careful
2. Knowledge about "Package Manager console" here and here
3, the above coding mode is "code First", "code before the current", English better Remember code first
Related information:
Http://msdn.microsoft.com/zh-cn/data/jj193542.aspx
http://www.cnblogs.com/qouoww/tag/Entity%20Framework/
4, the Database migration related information:
http://blog.csdn.net/vaivxuanzi/article/details/11633255
Http://msdn.microsoft.com/zh-cn/data/jj554735.aspx,
The next thing to do is to add content, not long I will raise salary, when the general manager, as CEO, win white Rich beauty, embark on the pinnacle of life! Think of a little excitement ~ ~