Database-generation model of the Entity framework

Source: Internet
Author: User

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.

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.