I also want to talk about the two methods of database priority and code priority in Entity Frame Work 4 (1)

Source: Internet
Author: User

In Entity Frame Work (hereinafter referred to as EF), two methods are supported: Database priority and code priority;

EF is An ORM framework launched by Microsoft. Before that, Java had a very mature ORM framework, such as hibernate. in the. NET version, the name is nhib.pdf, n more). I will only talk about EF this time, And nhib.pdf will talk about it again next time;

 

Let's talk about database first. Database first refers to creating a database first, including creating tables and fields, and then generating the ORM code based on the database. It refers to creating a database first, create related program code;

Next, let's take a look at the specific operations.

First, create a database and tables in a database, such:

 

Create an "ASP. net mvc 2 empty web application" with the project name "rorowodatafirst", for example:

 

After a project is created, vs2010 automatically creates a directory structure and basic file resources based on MVC 2, such:

 

Now we create a database-first ORM model for this project. Right-click the project and choose "add"> "new item". In the displayed dialog box, select "data ", then create an "ADO. net object data model ", such:

It will create a model1.edmx file by default, and then go to the next step, will let you choose whether to generate from the database or from the conceptual design model, here we choose database generation, (note: the conceptual design model is designed using the toolbox in vs2010, which is the code-first method we will talk about later). Click Next and we will choose database connection, if you have not created a database connection, there will be a new connection next to it. click the button to create a new connection. Here, my connection information is as follows:

 

Select to include sensitive data in the connection string.

Select the tables and views to generate.

The object model graph after the creation is successful.

 

After the controller is created, how can we use it? Create a default controller and the code is as follows:

 

Code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. MVC;

Using rorowodatafirst. models;

Namespace rorowodatafirst. Controllers
{
Public class defaultcontroller: Controller
{
//
// Get:/default/

Public actionresult index ()
{
// Use the context object of the orm object framework
Rorowodbentities context = new rorowodbentities ();

Blogcategory model = new blogcategory ();
Model. catename = "first category ";
Model. createtime = datetime. now;

// Add a category to the database
Context. addtoblogcategory (model );
Context. savechanges ();

Return view ();
}

}
}

 

Is it very convenient and powerful? If you have used hibernate, do you think it is very easy to get started?

Next time, I will talk about how to create an ORM in the code-first mode.

 

 

 

 

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.