Introduction
EF is relative to the dapper, nhibernate the official most of the ORM framework, its development process of convenience, fast and unquestionable, but because of the EF itself, some caching mechanism, tracking mechanism, so in use some places need special attention.
I'll share some of the lessons I've summarized in the project to help you use EF correctly.
Body
1. Inserting Data
Non-Recommended Practice:
DBCONTEXT.ENTITY.ADD (_entity); DbContext. SaveChanges ();
The right approach:
DbContext. Entry<tentity> (entity). state = Entitystate.added;dbcontext. SaveChanges ();
. SaveChanges () returns an int value for the number of affected bars
2. Updating Data
Non-Recommended Practice:
var entity = DbContext. Set<tentity> (). FirstOrDefault (predicate); predicate represents a lambda expression for a unique query, and when a parameter query is not unique, the secondary expression takes only the first entity.t_name = "32";d Bcontext. SaveChanges ();
Note: This must be done by passing in the primary key and re-querying again to prevent the addition of the other. The method of Asnotracking () is mixed. The function of asnotracking () is to discard the tracking of the EF object so that it is called after the property is re-assigned. SaveChanges () will be invalidated.
The right approach:
var entity = DbContext. Set<tentity> (). single (predicate); predicate represents a lambda expression for a unique query, and when the parameter query is not unique, an error Entity.t_name = "32";d Bcontext. Set<tentity> (). Attach (Entity);d Bcontext. Entry (entity). Property (A = A.t_name). IsModified = true; Set the management status of EF to T_name to be an update dbcontext.savechanges ();
The above approach will inevitably cause the ismodified to be set repeatedly if multiple fields are to be changed.
We further make a package for the modification method, which is encapsulated as follows:
public int Update (TEntity entity) { dbcontext. Set<tentity> (). Attach (entity); Propertyinfo[] Props = entity. GetType (). GetProperties (); foreach (PropertyInfo prop in props) { if (prop. GetValue (entity, NULL)! = null) { if (prop. GetValue (entity, NULL). ToString () = = "") DbContext. Entry (entity). Property (Prop. Name). CurrentValue = null; DbContext. Entry (entity). Property (Prop. Name). IsModified = true; } } Return DbContext. SaveChanges (); }
TEntity: Represents a generic class, a friend who is not familiar with a generic class can baidu a generic class or read this article in C # generic class, generic method, generic constraints actually apply complement.
Then I will give a detailed introduction to the EF processing mechanism, to provide reference, but also a summary of their own knowledge experience!
PS: Welcome to scan the QR code below, join QQ Group
Jacky Source: https://www.cnblogs.com/ydcnblog/statement: This article copyright belongs to the author and the blog Park is shared, welcome reprint, but without the author's consent must retain this paragraph statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.
asp: Native EF Insert, the correct way to update data