ASP MVC5 Introductory Learning series ④

Source: Internet
Author: User

Original: ASP. MVC5 Introductory Learning Series ④

Add model and simply use EF

For EF (entityframework) do not know the friends can go to Baidu liberal arts or in the garden search some simple resources to see, if and I know the concept of EF, then know that EF has a code first features, what meaning it! The simple and straightforward point of speaking is: map to the database by code! Some friends will say this is not add model say this EF what to do! This code first is where we add the model at the same time I'm going to use EF's code to map our model to the database so we can get the database out at once!

Next, let's add a model with the following:

We add a movie entity class that has the following properties:

OK, when our entity classes are created, what we need to do now is to map the model to the database, and each property of this model corresponds to the Latitude (field) of the Database model table. Now we need to do this with EF, and we need to use the base class DbContext to help us do something like this:

We have added a series of actions such as Moviedbcontext this class inherits DbContext and then through dbset<> to complete the mapping object, and then we map this to our database, for example, I have a local empty data as follows:

And then I'm going to map this model to this empty database, what do we do! In fact, it is very simple to configure a single sentence in the Web. config, open the Web. config to find the connectionstrings node, configure the following information:

Note that the name of the connection string to be added here must be the same as the class name of the preceding utility EF, otherwise you will not see the table mapped to the database, and when this is added, we can open VS's service Explorer with the following effect:

By doing the above steps we can use the strongly typed model to access the data in the controller, and you will find that everything becomes so straightforward. Let's add a moviecontroller with the following details:

Here we choose to use EF brackets.

Then click the Add button. Of course click Add after the VS has been the default implementation of the curd operation, the novice's words do not do so or step by step to their own experience it! I am here purely for demonstration and lazy, add finished we request our Movicecontroller, the specific operation is as follows:

OK, when we add a piece of data this is when we open our SQL Server database, open our just empty library, see if we add this data to map to our database, as long as the data mapped in so that our model entity should be mapped into a table, The effect we see when we open SQL Server is as follows:

OK, let's share the simple application of model and EF code frist here! In the back we continue to learn the entire MVC introductory series ...

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.