As we all know, since the 4.x version, the ENTITY framework has supported database first, model one, and code three generation patterns, and in the previous blog we learned about the code first ( There is also a pattern called codeonly, in contrast, code first is a more advanced technique, and we just need to write it. This time we go back and look at the simplest pattern in the EF framework, which is database first.
As the name suggests, Databasefirst is a database-centric development approach, using this pattern we must first design and create a database, Then use VS to create the Ado.net Entity Data Model on the basis of the existing database, then you can use the EF to access and manipulate the data in the database in the process of programming, and how to use it, the following is a very simple example.
First, we want to 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, as shown in the following figure
Second, next open VS2012, follow the steps of the following diagram to establish EDMX files,
Third, write test code, we insert 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 a database access Gateway
using (DBExamEntities1 examentity =new DBExamEntities1 ())
{
//create studentinfo an entity
t_ Studentinfo student = new T_studentinfo ();
Student. Studentno = "the";
Student. Studentname = "Li Seek Huan";
Student. Sex = "male";
Student. Grade = "University first grade";
Student. Age = "n";
The entity to be created, put 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, see if the insert succeeded in the database, as shown in the following illustration, to insert the data successfully
Summary: Mother doubt, EntityFramework is a very convenient and powerful ORM framework, and database first is a simpler way of development in three modes, the biggest advantage of this model is that the database can be reused, the premise of a database, development efficiency is a lever.