Since version 4.x, Entity Framework supports Database First and entity4.x.
As we all know, Entity Framework from 4. after Version x, Database First, Model First, and Code First generation modes are supported. In the previous blog, we learned about the Code First (also called CodeOnly) mode, code First is a relatively advanced technology. We only need to write Code. This time, let's look at the simplest mode in the EF framework, that is, Database First.
As the name implies, DatabaseFirst is a database-centric development method. To use this mode, we must first design and create a database, and then use VS to create an ADO database based on an existing database. NET object data model, and then you can use EF to access and operate the data in the database during programming. The following is a very simple example.
First, we need to create an example database, because this is not the focus of our discussion, so I will directly find an existing database for example, as shown in
2. Open VS2012 and follow the steps to create an edmx file,
3. Write the test code. We will use inserting data as an example to add a record to the StudentInfo table. The Code is as follows:
[Csharp]View plaincopy
- <Span style = "font-size: 18px;"> namespace Database_First
- {
- Class Program
- {
- Static void Main (string [] args)
- {
- // Create a database Access Gateway
- Using (DBExamEntities1 examentity = new DBExamEntities1 ())
- {
- // Create a StudentInfo object
- T_StudentInfo student = new T_StudentInfo ();
- Student. StudentNo = "110 ";
- Student. StudentName = "Li xunhuan ";
- Student. Sex = "male ";
- Student. Grade = "first year of university ";
- Student. Age = "21 ";
- // Put the created object into the collection of data entities of the Gateway
- Examentity. T_StudentInfo.Add (student );
- // Write back to database
- Examentity. SaveChanges ();
- }
- Console. WriteLine ("OK ");
- }
- }
- } </Span>
After running the program, check whether the data is successfully inserted in the database, as shown in.
Conclusion: This is an extremely convenient and powerful ORM framework, and Database First is a simple development method among the three modes. The biggest advantage of this mode is that it can reuse databases, on the premise of a database, the development efficiency is superb.