I. Creating an Entity Data Model
1. New Project > select project, add > New Item, select the ADO Entity Data Model in graph
2. Select generate from Database, next
3. Click New Connection
4, in the open window fill in the server address (Local is:.), fill in the database login user name and password, select the database, test the link is successful, click OK
5. Select Yes, next
6, depending on the circumstances, I use a data sheet, click to complete the good. This is the first step to create a good one.
7, because I put it in other projects, so I want to add it in. Add Reference
8. Add the database connection statement to the configuration file, Web. config
9. If we modify the database after creating the model, we need to update
10, we can also choose to modify the model structure in VS, and then update it to the database. EDMX file Right-click, select Build Database from model
11. Open the Database connection
12. On the generated SQL file, right click on Execute.
Second, with LINQ simple Application
1. Enquiry
Create an Entity Action object
DB_LXEntities db = new DB_LXEntities();
Get a piece of data
var user = db.T_User.FirstOrDefault(u=>u.Id==2);
Get full table data
var us = db.T_User.ToList();var us2 = db.T_User.Find();
Conditional query
var us = db.T_User.Where(u => u.age18);
Data paging
var us = db.T_User.OrderByDescending(o => o.Id).Skip(num).Take(30);
2. Insert
DB_LXEntities db = new DB_LXEntities();T_User user = new T_User();user.UserName"test";user.uPassword"test";user.age18;db.T_User.Add(user);db.SaveChanges();
3. Modification
DB_LXEntities db = new DB_LXEntities();var user = db.T_User.FirstOrDefault(u=>u.Id1);user.age25;db.SaveChanges();
4. Delete
DB_LXEntities db = new DB_LXEntities();var user = db.T_User.FirstOrDefault(u=>u.Id==2);db.T_User.Remove(user);db.SaveChanges();
Entity Framework creation and use (figure)