With Sberry, let’s dive in to clear out the confusion around the Add, Attach and Entry, Create and New methods of DbSet and DbContext of Entity framework.
public virtual object Add( object entity)
Adds the entity to the context specified if the entity is not already present in the context. If it is already present in some other state in the context then it’s state will be set to added. If an entity is present in the added state in context then calling Add is a no-op.
No-op stands for no operation is a computer instruction that takes up small space but performs nothing.
Adds the given entity to the context underlying the set in the Added state such that it will be inserted into the database when SaveChanges is called.
Note that entities that are already in the context in some other state like unchanged will get their state set to Added on calling the Add on the set. Add is a no-op if the entity is already in the context in the Added state.
If the entity is not present in the context then Attach() will attach the entity to the context and its state set to unchanged. If the entity is already present then Attach() is no-op.
After attaching the entity we can modify its desired properties without making the entire entity dirty. When savechanges() is called then only those properties that have been modified will be saved back to the database which will increase the performance.
Attaches the given entity to the context underlying the set. That is, the entity is placed into the context in the Unchanged state, just as if it had been read from the database.
public virtual object Attach(object entity)
Attach is used to repopulate a context with an entity that is known to already exist in the database. SaveChanges will therefore not attempt to insert an attached entity into the database because it is assumed to already be there. Note that entities that are already in the context in some other state will have their state set to Unchanged. Attach is a no-op if the entity is already in the context in the Unchanged state.
Creates a new instance of an entity for the type of this set. Entity instances can also be created by calling new, however, the distinction between the two is Create returns the instance which is a proxy of the entity if the underlying context is configured to create proxies and the entity type meets the requirements for creating a proxy.
These proxies will enable to use navigation properties to achieve some Entity Framework features such as lazy loading.
Note that this instance is NOT added or attached to the set.
public virtual object Create()
public virtual object Find(params object keyValues)
Finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.
Gets a DbEntityEntry object for the given entity providing access to information about the entity and the ability to perform actions on the entity. Calling Entry() on an entity makes the entity dirty such that whenever saveChanges is called the entity is updated to the database. This will create an update statement such that all the properties are considered in the query.