The database first generation mode of the Entity framework

Source: Internet
Author: User

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

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.