Entity Framework-code first of ASP. NET MVC

Source: Internet
Author: User

Recently, I used ASP. net mvc Entity Framework to develop some projects, which makes it very easy to use. As a result, I have witnessed the development of Microsoft technology:

-In this figure, we can clearly see the changes in data access methods.

If you want to know about ADO. NET Entity Framework, you can directly access the Entity Framework of this website.

If you want to know ASP. net mvc, you can directly access this Website: MVC

Here I will introduce some usage of Entity Framework:

 

In the garden, I have seen a lot of instructions on MVC, many of which use Model First, or Schema First. Here, I will combine ASP. NET
MVC 3.0 is a demo of Entity Framework code first. I hope you can help. This is especially helpful when you want to build small projects.

Lab environment:

OS: Windows Server 2008R2, Windows 7

DE: VS2010 + MVC 3.0 + Entity Framework + SQL Server 2008 R2

1. Create MVC
3.0 of website projects, which are the same as other MVC projects. Note: before creating an instance, remember to install MVC 3.0 and Entity Framework. After the instance is created, you will see the classic MVC mode, Model-> View-> Control

2. Add two classes to the Model. One is "Restaurant" and the other is "OdeToFoodDB,
Definition:

Restaurant

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;

Namespace MVCET. Models
{
Public class Restaurant
{
Public int ID {get; set ;}
Public string Name {get; set ;}
Public string State {get; set ;}
Public Adress {get; set ;}

}
}

 

OdeToFoodDB

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Data. Entity;

Namespace MVCET. Models
{
Public class OdeToFoodDB: DbContext
{
Public DbSet <Restaurant> restore ants {get; set ;}
Public DbSet <Reviewers> Reviewer {get; set ;}
}
}

OdeToFoodDB. Note that this class must inherit DbContext.

Then, add a database connection string in webConfig as follows:

<Configuration>
<ConnectionStrings>
<Add name = "ApplicationServices"
ConnectionString = "data source =. \ SQLEXPRESS; Integrated Security = SSPI; AttachDBFilename = | DataDirectory | aspnetdb. mdf; User Instance = true"
ProviderName = "System. Data. SqlClient"/>
<Add name = "OdeToFoodDB"
ConnectionString = "data source =. \ SQLEXPRESS; Integrated Security = SSPI; initial catalog = OdeToFoodDB ;"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>

3. Add the categorantcontrol class to Control, add View for the Index () function, and select a strong type to display data. In this case, the view automatically binds the data to display the data. At this time, there must be no data. How can we get the data? At this time, the database is generated according to the code.
4. Select the Server Browser window in VS and select "Add Connection..." on the database node .... "The Chinese version is" Add new connection ". Enter the database server name and enter the database name, as shown in:

Click OK and create a database. If a dialog box pops up, select "Yes. At this time, the system will automatically generate a database, as shown in

We can see that the field in the external ants table is basically similar to the name of the field in the Restaurant we define.
So far, we have generated a database based on the code structure. However, if the field in the Restaurant is changed, what should we do? At this time, it is necessary to Drop the previous database, and then re-create the database
5. changes have taken place in the category and the database has also changed accordingly:
In the Application_Start () function of Global. asax, add the following code:

Protected void Application_Start ()
{
// Database. SetInitializer (new DropCreateDatabaseIfModelChanges <OdeToFoodDB> ());
Database. SetInitializer (new OdeToFoodDBInitializer ());
AreaRegistration. RegisterAllAreas ();

RegisterGlobalFilters (GlobalFilters. Filters );
RegisterRoutes (RouteTable. Routes );
}

You can also write a class that inherits DropCreateDatabaseIfModelChanges <OdeToFoodDB>. As shown in the following code, you don't need to comment on a line of code.

OdeToFoodDBInitializer

Public class OdeToFoodDBInitializer:
DropCreateDatabaseIfModelChanges <OdeToFoodDB>
{
Protected override void Seed (OdeToFoodDB context)
{
Base. Seed (context );

Context. Merge ants. Add (
New Restaurant ()
{
Name = "qitian ",
Adress = new Adress () {Street = "Nanjiang RD, MD", City = "Shanghai "}
});

Context. Merge ants. Add (
New Restaurant ()
{
Name = "haiyun ",
Adress = new Adress () {Street = "Dongchuan RD, MD", City = "Shanghai "}
});
Context. Merge ants. Add (
New Restaurant ()
{
Name = "qitian ",
Adress = new Adress () {Street = "Beijing RD, HF", City = "Guangdong "}
});

Context. Merge ants. Add (
New Restaurant ()
{
Name = "Lantian ",
Adress = new Adress () {Street = "Wuhan RD, HF", City = "Guangdong "}
});

Context. Reviewer. Add (new Reviewers (){
Name = "Tomin", Sex = "Female "});
Context. Reviewer. Add (new Reviewers ()
{

Name = "Tony ",
Sex = "Male"
});

Context. SaveChanges ();

}
}

Re-running the application will give you an unexpected result and you will be stunned.

MVCET.zip

 

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.