Entity Framework Quick Start -- ModelFirst, entity -- modelfirst

Source: Internet
Author: User

Entity Framework Quick Start -- ModelFirst, entity -- modelfirst

Entity Framework not only facilitates operation, but also considers user friendly interaction. The perfect combination of EF4.0 and vs2010 is also a reason for us to choose it. Compared with Nhibernate Microsoft, this is indeed a good job.

The following is a simple example of CodeFirst.

We still create a console project like the previous article Entity Framework Quick Start-instance.

Step 1: Create a console Project

Step 2: Right-click the project and add the Ado. Net object data model.

:

Step 3: select the method for generating the object "Empty model"

:

Step 4: Add EF entities

Drag an object from the toolbar to the edmx designer and Name it Student. Then, on the property page, modify the object set Name of this object as Student and add two scalar property names, age [complex attributes and navigation attributes will be introduced in the following article], as shown in:

Step 5: generate a database based on the model

Note: Right-click the edmx blank area, select generate database based on the model, and select the database connection we want to create. As shown in:

Click "OK" and click "Next". The generated SQL statement based on the model is displayed. On the generated SQL page, right-click and execute the SQL statement, as shown in:

Check that SchoolDB and Student have been created for us in the database, as shown in:

This is a great pleasure. This automation tool is indeed very easy to use and enjoys such silly operations provided by Microsoft.

Step 6: Use EF to insert a piece of data

First, let's take a look at the code automatically generated by EF: Mainly Database Access Gateway and entity class Student, as shown in:

Then add the following code and execute

 

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;

Namespace EFCodeFirst
{
Class Program
{
Static void Main (string [] args)
{
Using (SchoolContainer schoolEntities = new SchoolContainer ())
{
// Create a student instance
Student stu = new Student {Age = 23, Name = "Flydragon "};
// Put the above instance into the entity set corresponding to the Gateway
SchoolEntities. Student. AddObject (stu );
// Persists to the hard disk
SchoolEntities. SaveChanges ();
}
Console. WriteLine ("OK ");
}
}

The program runs successfully. View the database:

The result is correctly executed.

To sum up, I will first add an entity model, then add an entity class above, and generate a database directly based on the SQL generated by the model, then, you can directly use the ObjectContext generated by ef and the database entity Student to directly operate the database table. This is also an attractive part of CodeFirst programming.

What Microsoft can do so conveniently and easily is indeed admirable!

 

Entity Framework Quick Start-index stickers

Reference page: http://qingqingquege.cnblogs.com/p/5933752.html

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.