Entity framework4.0 (2) Overview (EF4 model first method)

Source: Internet
Author: User

EF4 supports three construction methods: 1. Database first. 2. model first method. 3. Code first method. Developers can select any method based on specific project conditions.

The method of database first was quickly demonstrated last time. This demonstration demonstrates the model first method.

If you have experience using power designer for data modeling, you should be familiar with this method. It doesn't matter if you haven't touched the Power Designer before. After all, these are all manual operations. You can check it again. Haha.

All right, let's talk less about it, and the book goes back to the beginning.

========================================================== ========================================================== ================

1. Start vs2010. create a console application named efmodeldemo, for example:

2. on the project, right-click = and select "Add-> new items...". For example, select ado.net Entity Data Model: Name: School

3. Select "Empty model" and click Finish. For example:

4. The EDM designer interface is automatically opened. For example:

5. Drag "entity" to the interface designer. An object is generated. For example:

6. Add other attributes to the three entities, for example. Right-click the designer and choose scalar property format --> display name and type. The type of the attribute is displayed.

7. By default, the property type is string. Select an attribute item (such as classid) in the object, and change its type to int32. (because it will correspond to the ID in the class object as a foreign key, so make sure that the two types are the same .)

8. Establish Association: In toolbox, click "association" with the left mouse button, and then you can release the mouse. "association" is still in the selected state. Move the mouse to the design panel. When the mouse is near the object, the mouse pointer changes to a line. Click the left button on the primary object, drag it to the slave object, and release the left button. A line is generated. Is Association. For example:

9. Modify student. The association between course is multiple-to-many (that is, * indicates multiple. * *). For example:

10. Double-click the class and set the associations between student as follows:

11. Because EF4 in model first does not help us create databases, it only helps us create tables. Therefore, we need to establish the database in advance:

12. At this time, there are no data tables in the database. Right-click the blank area on the EDM designer, for example:

 

Click OK, for example:

Click: Next. For example, the generated script is prompted.

Click: Finish. For example

After successfully connecting to the server, click execute script. The corresponding data table is generated in the database School.

Note: Observe the generated data table student. The course has many-to-many relationships. Therefore, an intermediate table studentcourse is generated. The primary key is the primary key combination of student and course.

In the student table, we designed a classid for the foreign key to associate the class object. This association is called a foreign key Association in EF4 (a special foreign key corresponds to the navigation attribute ). EF4 also supports another association: Independent Association (there is no special foreign key to correspond to the navigation attribute, EF4 does not have a foreign key, you can also track and manage navigation attributes ). Here, when there is no ORM framework, we adopt the traditional approach: Using Foreign keys to associate two entities.

========================================================== =====

Okay. First come here. Sorry, I have to rest. Good night !!!

 

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.