20 secrets of mvc3-(2) automatically generate controller and view through scaffolding-code first/database first

Source: Internet
Author: User
Tags sql client

 

Scenario
You want to manage dynamic content through your website

Solution

The scaffold is used to automatically generate one controller and multiple views, allowing users to create, read, update, and delete (crud) data. Use the entityframework code-first (CodeFirst) and database-first (database first) method.

Discussion

A model and dbcontext need to be created before using scaffolding to build contorler and view. It defines the type of data stored in the model. (This is the code first method)

In the following example. The created two classes provide the ability to manage a book list.

The first class contains the definition of book data, which will be stored in SQL express database.

The second class contains dbcontext, which creates the dbset of book class.

Create model, right-click the model folder. Select "add"> "class. Enter book. CS in filename and replace the generated code with the following code.

 Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Data. entity;
Namespace Mvcapplication. Models // Use your namespace here

{
Public Class Book
{
Public Int Id { Get ; Set ;}
Public String Title { Get ; Set ;}
Public String ISBN { Get ; Set ;}
Public String Summary { Get ;Set ;}
Public String Author { Get ; Set ;}
Public String Thumbnail { Get ; Set ;}
Public Double Price { Get ; Set ;}
Public Datetime published { Get ; Set ;}
}
Public Class Bookdbcontext: dbcontext
{
Public Dbset <book> books { Get ; Set ;}
}
}

After creating the Book Model and bookdbcontext. We can use scaffolding to build the controller and view.

Right-click the Controller folder. And choose "add"> "controller ".

For example:

 

Follow the selection template: Using entityframework.

Modelclass: select the book we just created.

Datacontext: Select bookdbcontext. Click "add ".

At this time, we will find that scaffolding has helped us create a bookcontroller. I found that all the actions have been written for us.

Note: I have created a new book by accessing localhost/book/index.

We can easily add, delete, query, and modify data. Is it convenient?

 

Entity FrameworkAlso provides the ability to build the controller and view through a different method. Database-first database first. This method can be used to generate a model using an existing database.

In large projects, it is quite common to use resources rationally on an independent basis. An excellent front-end engineer is not necessarily an expert in database design. Therefore, a database expert is required to design the database.

In the next example, we will design a database-first model.

Right-click the model folder. New-> item. Select ADO. Net object database model from the list on the right.

Name itBookdbfirstmodel. edmx.

Now it is time to establish a database connection.

1. Select generate from database

2. Click Create connection.

3. Default Data source: Microsoft sqlserver (SQL client)

4. Server Name:. \ sqlexpress

5. In the database name, select the mvcapplication. Models. bookdbcontext automatically generated for the previous case by using MVC. Click OK.

Save the object connection settings in Web. config as "bookentities ".

Select include tables, views, and stored procedures in the data model. Click Finish.

 

Now let's take a look at our model folder. A bookdbfirstmodel. edmx entity is added.

Compile the solution before using scaffolding.

Right-click the Controller folder. Add, such:

 

For model class, enter the books generated by Entity Framework.

Datacontext selects the object connection we just set to set bookentities ".

A new Controller: bcontroller is created. This is performed first by the database.

 

I made a demo. You can click Download to view the information you need.

For more information, see

ADO. NET Entity Framework Overview

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.