Use ASP. net mvc + Entity Framework to quickly build a blog System

Source: Internet
Author: User

I have been learning ASP. net mvc for some time. I plan to train a small program as a record and share it in the learning process.

First, you have to determine the demand. If there is a demand for wood, you can make a wool! Hmm ...... After a rough thought, we finally come up with the following requirements:

1. Customizable Classification

2. Post

OK! In this way, we should first start with a simple process (in fact, it is complicated and will not be done), and then add it later (hopefully the level can be reached ). If the function is determined, you need to determine what it will look like. Discuss with du Niang first ......

Finally, under my lust, du Niang handed over a relatively concise one. The source website is here (to express thanks). After being castrated by the younger brother, the effect is as follows:

 

Using System; using System. collections. generic; using System. linq; using System. web; namespace ShowPin. web. models {/// <summary> /// Category /// </summary> public class Category {public Category () {this. posts = new List <Post> () ;}/// <summary> /// obtain or set the category Title /// </summary> public string Title {get; set ;}//< summary >/// content set under this category //// </summary> public virtual ICollection <Post> Posts {get; set ;}}}

 

Post:

Using System; using System. collections. generic; using System. linq; using System. web; namespace ShowPin. web. models {/// <summary> /// blog // </summary> public class Post {/// <summary> /// get or set the content title /// </summary> public string Title {get; set ;}//< summary> /// get or set the Content /// </summary> public string Content {get; set ;} /// <summary> /// get or set the content release date /// </summary> public DateTime CreateDate {get; set ;} /// <summary> /// get or set the number of clicks /// </summary> public int hits {get; set ;} /// <summary> /// obtain or set the category ID /// </summary> public string CategoryId {get; set ;} /// <summary> /// obtain or set a Category /// </summary> public virtual Category {get; set ;}}}

 

Well, the next step is the legendary DBContext.

ObjectContext:

Using System; using System. collections. generic; using System. data. entity; using System. linq; using System. web; namespace ShowPin. web. models {// <summary> // data context // </summary> public class ObjectContext: DbContext {public ObjectContext (): base ("ShowPinContext ") {}/// <summary> /// Category list /// </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 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" />

Look at the following picture.

One or more verification errors are detected during model generation:

ShowPin. Web. Models. Category: EntityType "Category" does not define the key. Define a key for this EntityType.

ShowPin. Web. Models. Post: EntityType "Post" does not define a key. Define a key for this EntityType.

Categories: EntityType: EntitySet "Categories" is based on the "Category" of the undefined type of any key ".

Posts: EntityType: EntitySet "Posts" is based on the undefined type "Post" of any key ".

 

This is because no primary key is defined. Simply put, it is the legendary ID ...... Add a primary key to the object class

 

/// <Summary> /// primary key /// </summary> public string Id {get; set ;}

String-type primary keys are rare.

Again, enter Enable-Migrations-ContextTypeName ShowPin. Web. Models. ObjectContext and press Enter"

 

 

I'm a monkey. Are you kidding me ??

Try again !!

Enter Enable-Migrations-ContextTypeName ShowPin. Web. Models. ObjectContext-force and press Enter"

 

 

Haha, haha ......

He also generated some files to see what it is.

 

 

I have an additional Migrations directory and Configuration. cs file. What is this? Ignore him first.

Next, perform the CRUD operation on Category to create a Controller.

 

 

 

Click "OK" and T appears. Enter "http: // localhost: 10223/Category" in the browser.

The address is my local address. Please modify it according to your actual situation.

 

Sorry, an error occurred !!!

Because it was not updated after database migration was enabled, Let's update it next !! Open the Package Manager Console, enter Add-Migration ini, and press Enter"

 

 

An additional file is added to the Migrations directory.

 

 

What is it? Ignore it first, and then enter: Update-database

 

 

It seems successful. Check the App_Data directory.

 

After the database is generated, refresh the webpage.

 

 

Finally, haha ...... Next is Post.

Success!

Here is a summary:

1. Some of the above minor problems are basically caused by carelessness. Therefore, you must be careful in your next study.

2. Here is the knowledge about the "Package Manager Console ".

3. The above encoding modes are "code first" and "code first". It is easier to remember code first in English.

Related information:

Http://msdn.microsoft.com/zh-cn/data/jj193542.aspx

Http://www.cnblogs.com/qouoww/tag/Entity%20Framework/

4. database migration information:

Http://blog.csdn.net/vaivxuanzi/article/details/11633255

Http://msdn.microsoft.com/zh-cn/data/jj554735.aspx,

 

The next thing I want to do is add content. It won't take long for me to get promoted and raise my salary. When I become a general manager, I will become the CEO, win white rich and beautiful, and embark on the peak of my life! A little excited ~~

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.