MVC learning-use EF for addition, deletion, modification, and query, mvcef

Source: Internet
Author: User

MVC learning-use EF for addition, deletion, modification, and query, mvcef

There are two important methods in DbContext:

I. Several Methods

1. One is the Entry () method:

Public DbEntityEntry <TEntity> Entry <TEntity> (TEntity entity) where TEntity: class;

Any type of data can be put into the context object.

DbEntityEntry has an attribute EntityState that identifies the objects in the context and performs corresponding SQL operations according to the value of the identifier.

public enum EntityState    {               Detached = 1,             Unchanged = 2,              Added = 4,              Deleted = 8,                Modified = 16,    }

2. There is also a Set () method

Public DbSet <TEntity> Set <TEntity> () where TEntity: class;

// Use public DbSet <TEntity> Set <TEntity> () where TEntity: class; and
// Public virtual DbSet <Users> Users {get; set;} knows
// Set <Users> is the Dbset <Users> type. Therefore, context. Set <Users> () is equivalent to context. Users.

3. Descriptions of several methods in the operation:

SaveChange ():When SaveChanges is executed, EF traverses all entity objects in the container in the context, checks the State attribute of the object, generates corresponding SQL statements, and sends them to the database for execution at a time.
Knowledge points:
1. Objects in the context are tracked by default. When A extracts the Data and prepares to modify the Data, B can retrieve the Data but cannot modify it because the current tracker is on the side and waits until A releases the resource, b.
Ii. add, delete, modify, and query

1. Add

// 1. create a context object because the context object contains the object UsersMyDBEntities context = new MyDBEntities (); // 2. because the Entry needs to input an object, create the Users u = new Users () {Name = "kim", Age = 23}; // 3. add By tag
Context. entry <Users> (u ). state = EntityState. added; // 4. saved to the database because EF is delayed. If you do not perform the List () or other operations, the SQL statement context is not executed. saveChanges ();

2. Delete

2.1 Delete by Id (primary key of the table)

// 1. create an object Users u = new Users () {Id = 3}; // 2. the identifier is to delete the context. entry <Users> (u ). state = EntityState. deleted; // 3. save to database context. saveChanges ();

2.2 deletion based on non-primary keys

// 1. the condition to be deleted. Here is the string name = "Kim"; // 2. obtain the var s1 = from s in context object whose name is Kim. users where s. name = name select s; // 3. if there are more than one, use foreach () to traverse, and delete the first context here. users. remove (s1.FirstOrDefault (); // 4. save to database context. saveChanges ();

3. Change

// 1. obtain the data to be updated. In the Action Method in mvc, you can directly obtain the updated object Users u = new Users () {Id = 4, Name = "kim "}; // 2. the identifier is to modify the context. entry <Users> (u ). state = EntityState. modified; // 3. save to database context. saveChanges ();

4. Query

4.1 normal query

// Query all data named Kim var s = context. Users. Where (u => u. Name = "Kim"). Select (u => u );

4.2 querying by PAGE

/// <Summary> /// query by PAGE /// </summary> /// <typeparam name = "T"> type of data to be operated </typeparam> // /<param name = "whereLambda"> Where Condition Statement </param> // <param name = "orderLambda"> sort by conditions </param> // <param name = "pageSize"> fewer data entries per page </param> // <param name = "pageIndex"> page to query </param> // <returns> returns a generic set </returns> static List <T> GetPageList3 <T> (Func <T, bool> wherelamsize, Func <T, object> orderLambda, int pageSize, int pageIndex) where T: class {MyDBEntities context = new MyDBEntities (); var list = context. set <T> (). where (whereLambda ). orderBy (orderLambda ). skip (pageIndex-1) * pageSize ). take (pageSize); return list. toList ();}

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.