Entityframework learning notes -- 003-use model first, entityframework

Source: Internet
Author: User

Entityframework learning notes -- 003-use model first, entityframework

First, I personally think this (model first is model first) is a chicken-like function. While praising his powerful functions, you will also feel that this function is not very necessary and rarely used.

1. right-click your project, select "add", "new item", and select "data" and "ADO. NET object data model, and click "add ". 3-1

For example, 3-1

2. Select "Empty EF designer model" and click "finish ". 3-2

Figure 3-2

3. Right-click the designer, select Add object, add Object Name and other data items, and click OK. 3-3

 

 

Figure 3-3

4 ,. right-click the attributes of the "Student" object, choose "add"> "scalar attribute", change the attribute Name to "Name", and right-click "Name ", click "properties" to bring up the property box and set the corresponding property value. Add the "Age" and "Detail" Fields in sequence based on this method. 3-4

Figure 3-4

5. After the entity design is complete, right-click the blank area of the designer and select "generate Database Based on Model ". In the pop-up dialog box, you can select "New Connection" or a generated connection, and then click Next. 3-5

Figure 3-5

6. A. SQL file is generated here. Open the file, right-click the blank space, and click "execute ". 3-6. (If the SQL statement cannot be executed, copy the SQL statement content and execute it directly in the database.) 3-6

Figure 3-6

7. After the molding is successful, the tables in the database are generated. The following example shows how to add data to a database, which is the same as other methods.

1 using (Model1Container1 db = new Model1Container1 () 2 {3 for (int I = 0; I <5; I ++) 4 {5 Student s1 = new Student (); 6 s1.Name = "studnet" + I; 7 s1.Age = I; 8 s1.Detail = "detail" + I; 9 10 db. students. add (s1); 11} 12 13 int count = db. saveChanges (); 14 15 Console. writeLine ("{0} records inserted successfully! ", 5); 16 17 Console. writeLine ("name \ t age"); 18 19 foreach (var item in db. students) 20 {21 Console. writeLine ("{0} \ t {1}", item. name, item. age); 22} 23}
View Code

Output:

Figure 3-7

8. In fact, "model first" has more powerful functions than this, such as "Table relationships", "navigation attributes", and "attribute restrictions. However, there may not be many people using "model first" because of this. In ef7, this function has been removed.

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.