Since version 4.x, Entity Framework supports Database First and entity4.x.

Source: Internet
Author: User

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
  1. <Span style = "font-size: 18px;"> namespace Database_First
  2. {
  3. Class Program
  4. {
  5. Static void Main (string [] args)
  6. {
  7. // Create a database Access Gateway
  8. Using (DBExamEntities1 examentity = new DBExamEntities1 ())
  9. {
  10. // Create a StudentInfo object
  11. T_StudentInfo student = new T_StudentInfo ();
  12. Student. StudentNo = "110 ";
  13. Student. StudentName = "Li xunhuan ";
  14. Student. Sex = "male ";
  15. Student. Grade = "first year of university ";
  16. Student. Age = "21 ";
  17. // Put the created object into the collection of data entities of the Gateway
  18. Examentity. T_StudentInfo.Add (student );
  19. // Write back to database
  20. Examentity. SaveChanges ();
  21. }
  22. Console. WriteLine ("OK ");
  23. }
  24. }
  25. } </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.

Related Article

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.