Quickly build systems using the ASP. Mvc+entity Framework

Source: Internet
Author: User

More information: http://www.cnblogs.com/dingfangbo/p/5771741.html

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 a little bit.

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>///Classification///</summary>PublicClassCategory {PublicCategory () {This. Posts =New list<post>(); }///<summary> //////</summary> public string Title {set///<summary> // ////</ summary> public virtual icollection<post> Posts {get; set       

Post:

UsingSystem;UsingSystem.Collections.Generic;UsingSystem.Linq;Usingsystem.web;Namespaceshowpin.web.models{///<summary>///Blog///</summary>PublicClassPost {///<summary>///Gets or sets the content title///</summary>PublicString Title {GetSet; }///<summary>///Gets or sets the content///</summary>PublicString Content {GetSet; }///<summary>///Gets or sets the content release date///</summary>Public DateTime CreateDate {GetSet; }///<summary>///Gets or sets the number of hits///</summary>Publicint hits {GetSet; }///<summary> //////</summary> public string CategoryId { get; set///<summary> // / Get or set classification ///</ summary> public virtual category 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>PublicClassObjectcontext:dbcontext {PublicObjectContext ():Base"Showpincontext"///<summary > //////</summary> public dbset<category> Categories { get; set///<summary> // / blog list ///</summary > public dbset<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        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 ~ ~

Quickly build a blogging system using the ASP. Mvc+entity Framework, Bubu Buckle, bubuko.com

Quickly build a blog system using the ASP. Mvc+entity Framework

Original: http://www.cnblogs.com/vin-c/p/3731068.html

Quickly build systems using the ASP. Mvc+entity Framework

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.