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

Source: Internet
Author: User

Last time I talked about how to create an ORM using EF's "database first" method. Today I will talk about "code first" method to create an Orm;

Literally, the first priority of a database is to create a database. The first priority of a code is to write code first. Therefore, the first priority of a code is to write code first, that is, it is the opposite of database priority.

 

First, open vs2010 and create an empty ASP. net mvc 2 project named rororowocodefirst. For example:

The project structure after successful creation, such:

 

Now we create a code-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 Toolbox design conceptual model, here we choose to generate from the Toolbox design conceptual model.

After the selection is complete, a blank interface will appear. The Toolbox on the left contains the tools we want to use, such:

 

From the toolbox, we use the "entity" tool to drag an "entity" to the blank space, such:

By default, the system creates a class named "entity 1" and has an ID attribute field. Double-click over it to modify "entity 1, change "entity 1" to "blogcategory", add the attribute to it, and change it to the following result:

Now, let's check the code. The development tool has helped us generate the ORM code, for example:

The above is the code generated based on our design. Of course, you can also manually write the code without using the design tool. ^ _ ^ is also acceptable. Next, how to create a database? See the following figure:

At this point, we can right-click the interface according to the design, and select "generate Database Based on Model" to obtain the database SQL code generated by the development tool based on our code, used to create a database. Next time, I will talk about the POCO method.

 

 

 

 

 

 

 

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.