Record the imprint of my ASP. NET Mvc+ef

Source: Internet
Author: User

First of all, I think we are not unfamiliar with the ASP. Mvc+ef, sadly, I knew from 2012 that his existence had not been studied until now. Can be said to be a lot of waste of time, because the landlord has been using the ado.net+ three layer mode, so always feel that there is a world of their own, at the same time, I also call for the same as the Orange Ape or siege division please face it, the footsteps of the times will not stay because of you. But for the last time in the second half of 2014, the entire market is using ASP. NET Mvc+ef or other persistent layers to build the project. The general trend has to face it.

The landlord's MVC level still stays in the level of 2.0, imagine. It was used for project needs, so far it is not familiar to the level. The corresponding EF so-called entityframework is not know what is what, only know is an ORM, this is the small white concept of the landlord. It can be said that many have just learned that his little companions are of the same level. Not much to say this nonsense, we went into the Learning Avenue.

First of all I use or VS12 corresponding to use VS15 's small partner may be a bit different, after all, the version of some of the things are still a bit out.

  

Can see his comments for the construction of an ASP. NET MVC4 or Web API, there is a Web API I think you know a little bit about it, Web API is generally used for interface development.

  

We start with the system default, because we're not going to do anything so look at how Microsoft is initializing one for us, and then you can see that the project is neat. The red part is a few important files under MVC.

  

You can open the App_start file and see what it's called. Routing mechanism

  

Routeconfig,webapiconfig look for these two, other people can go to understand the next.

Then we take a look at what Microsoft has introduced to us, and you can open the reference to see what's inside, and you can see that a lot of DLLs are referenced including entityframework

  

  

You can see that the VS12 comes with a version of EF 4.0, and then everyone opens the path to the package to see if there is more than one folder called packages, in the root directory. It's Microsoft's recommended extensions and the files that EF needs.

In order to clean everyone can put controllers folder under the Accountcontroller,filters folder, view under the Account folder and models under the Accountmodels are deleted, and then F5 compiled and opened to see , then some people will error "MYSQL.WEB.V20" Such errors, because I tried, the specific reason is that the version is wrong, find a solution, proposed to put the system under the disk C:\Windows\Microsoft.NET\Framework\ V4.0.30319\config\machine.config file, search for the next two places containing the MySql.Web.v20 field to "Mysql.web" Yes, get rid of it. V20 do not know whether this method is correct, anyway, after the correct use, if there is improper please point out. That's how it works.

  

As for the basis and grammar we still find some books to see, after all, the landlord is also a small white many do not understand, I can only introduce the first how to build a project to see what is a thing.

Then create the entity class under the models file film

  

    <summary>    ///Movies    ///</summary> public class Film    {        [Key]        [Display (name=] Number ")] public        int ID {get; Set }        [Display (name= "movie name")]        [Required (errormessage= "*")] [        stringlength (+)] public        string Title { Get Set }        [Display (name= "clicks")]
[RegularExpression (@ "^\+?[ 1-9][0-9]*$ ", errormessage=" can only enter a positive integer, by [0-9] The number combination must not be 0 start ")] public int Clicknum {get; set;} [Display (name= "Creation Time")] [DataType (Datatype.datetime)] Public DateTime creatdate {get; set;} }

As for the above attributes, let's talk about it roughly. These properties are all in a namespace called System.ComponentModel.DataAnnotations key, presumably everyone understands that this is the meaning of the key, yes, the meaning of the definition of the primary key, display comment description, required must verify, stringlength field size , the datatype type.

Then we add a contexts folder to the project, directly and add a context FilmEntities.cs

Using system.data.entity;using mymvch.models;namespace mymvch.contexts{public    class Filmentities:dbcontext    {public        dbset<film> filecontxt {get; set;}}    }

Ok this next we compile the project, in order to pave the next step.

Then this controller folder right-click Add Director

  

If you do not have an error in the Controllers folder to survive Filmcontroller, such as

  

If you have an error, please check if your EF framework supports the current version, then we will change the configuration file of Web. config primarily to change the way data is connected

  

Change to their own database connection method is recommended is an empty database, such as the landlord's

  

Then we go to the _layout.cshtml under the shared folder under view to change the link

  

"Home" can be changed into a movie, home is corresponding to your entity name film, compile and browse again

  

Click to look inside

  

Check to see if the data has the database you need, and whether it has been automatically created related

Run the interface

Can see the background of the field comments are on the page program came out, add a look.

Now, I suddenly think of the entity also missed an important attribute, the unit price forgot to add, meaning we in the entity added Unit Price field as follows

[Display (name= "price")] [RegularExpression (@ "^[0-9]\d*\.? \d*[1-9]\d*$ ", errormessage=" Please enter the correct price ")]public decimal prices {get; set;}

Here we go first to live, hold and hold. Here first introduce, I use the development model, this instance is using the ASP. NET mvc4.0+ef4.0 and is code first mode, and in the current, if you compile and run the project now, then what will happen, Yes, the new data we submitted before will be deleted, because the system it received, your entity has a new change, so he will go directly to the new table, which is not allowed in the actual development, then how to version, Microsoft gave us code first to migrate the update database.

You can update the database by following these steps to eliminate the above exception:

1, in the project "Package Management Console" window, enter "Enable-migrations", carriage return, resulting in a project to generate a "migrations" folder, which contains two. CS class.

2. The code in the Modify constructor in the Configuration.cs generated in 1 is: "automaticmigrationsenabled = true;"

3, save the changes, in the "Package Management Console" Enter "Update-database", carriage return, run again.

The above steps can be used to save the original data, but also change the table structure.

As I study the steps, I hope to give you help, this completed is the level of just getting started, Daniel do not spray, there is insufficient please refer to the wrong, after all, the younger brother has just begun to learn.

As for why this writing, visual inspection of the whole garden is the basic theory, theory, and then the theory, corresponding to the need to understand how to achieve the small partners have a bit that, not to say that the theory is not important, just let him know how he presented, in-depth study!!

Record the imprint of my ASP. NET Mvc+ef

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.