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 );}