Extended usage of EntityFramework. Extended

Source: Internet
Author: User

EntityFramework. Extended is an extension method based on the EntityFramework IQueryable type, including Update and Delete.

Its advantage is that the modification and deletion operation not only has an Id condition, but also has a condition;

During modification, you can not only pass in the entire object type, but also pass in the content of the local field to be changed according to the condition.

1. Nuget package management search and download assembly

2. Create a data context

/// <Summary> /// data context /// </summary> public class MyDbContext: dbContext {# region constructor // <summary> // initialize a new instance that uses the connection name "default" to access the context class. // </summary> public myDbContext (): base ("SqlServer ") {}/// <summary> /// initialize a new instance of the context class using the specified data connection name or connection string. /// </summary> public SchoolDbContext (string nameOrConnectionString): base (nameOrConnectionString) {}# endregion # region attribute public DbSet <Member> Member {get; set ;}# endregion}


3. Call Method

Before referencing a namespace

Using EntityFramework. Extensions;

Note that this is an IQueryable extension method;

Modify the local field content:

Public bool ModifyName (int memberId, string name, string newName) {using (MyDbContext context = new MyDbContext () {int state = context. member. update (m => m. id = memberId & m. name = newName, // modify condition m => new Member {Name = newName}); // modify only Name return state> 0? True: false ;}# endregion


Delete operation:

Public bool Delete (int memberId) {using (MyDbContext context = new MyDbContext () {int state = context. member. delete (m => m. id = memberId // modify the condition); return state> 0? True: false ;}}

It is more convenient to encapsulate it into the Repository warehouse operation class.

/// <Summary> /// obtain the query dataset of the current object /// </summary> public virtual IQueryable <TEntity> Entities {get {return EFContext. set <TEntity> ();}}

 

/// <Summary> /// delete all data that meets the specified expression /// </summary> /// <param name = "predicate"> query condition predicates </ param> // number of rows affected by the <returns> operation </returns> public virtual int Delete (Expression <Func <TEntity, bool> predicate) {return Entities. delete (predicate );} /// <summary> /// modify the operation // </summary> /// <param name = "fun1"> query condition-predicate expression </param> // /<param name = "fun2"> Object-predicate Expression </param> // number of rows affected by the <returns> operation </returns> public virtual int Update (Expression <Func <TEntity, bool> fun1, Expression <Func <TEntity, TEntity> fun2) {return Entities. update (fun1, fun2 );}

 

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.