As we all know, since the 4.x version, the Entity framework began to support the database first, model three generation mode, in the previous blog, we learned about the code There is also a pattern called codeonly, which, in comparison, is a more advanced technology, and we just need to write code. This time we go back and look at the simplest pattern that the EF framework personally considers to be the database first.
As the name implies, Databasefirst is a database-centric approach to development, which we must first design and create a database, Then using VS to create the ADO Entity Data Model based on the existing database, you can then use EF to access and manipulate the data in the database during the programming process, using it in a simple example.
First, we will create a sample database, because this is not the focus of our discussion, so the author directly from the existing database to find an example, such as
Second, next open VS2012, follow the steps to create an edmx file,
Third, write the test code, we insert the data as an example, to the Studentinfo table to add a record, the code is as follows
<span style= "FONT-SIZE:18PX;" >namespace database_first{ class program { static void Main (string[] args) { //CREATE DATABASE Access Gateway using (DBExamEntities1 examentity =new DBExamEntities1 ()) { //create studentinfo an entity t_studentinfo student = new T_studentinfo (); Student. Studentno = "a"; Student. Studentname = "Li Xun Huan"; Student. Sex = "male"; Student. Grade = "University first grade"; Student. Age = "+"; Creates an entity that is placed into the gateway's collection of data entities examentity. T_studentinfo.add (student); Write back to the database examentity. SaveChanges (); } Console.WriteLine ("OK");}}} </span>
After you run the program, see if the insert succeeds in the database, as shown in successfully inserting the data
Summary: The mother questioned, EntityFramework is a very convenient and powerful ORM framework, and the database first is a more convenient way of development in three modes, the biggest advantage of this model is the reusable database, in the premise of the database, development efficiency is the leverage.
The database first generation mode of the Entity framework