What is the different when query data from context using
Both return the entity being requested.
Some example I found on Microsoft are using
SingleOrDefault variation to query for entity. Some uses
Are there any "performance" advantanges when using one over the other?
Although they look the same they are very different in some fundamental ways
Find starts by searching in the local cache of the context. If no match are found then it sends a query to the db.
Documentation is your friend
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.
Returns a single, specific element of a sequence, or a default value if no such element is found.
Returns the first element of a sequence, or a default value if no element is found.
The Find method on DbSet uses the primary key value to attempt to find an entity tracked by the context. If the entity is not found in the context then a query will be sent to the database to find the entity there. Null is returned if the entity is not found in the context or in the database.
Find is different from using a query in two significant ways:
- A round-trip to the database will only be made if the entity with the given key is not found in the context.
- Find will return entities that are in the Added state. That is, Find will return entities that have been added to the context but have not yet been saved to the database.
Does this mean that if the entitiy was already being tracked (through lazy loading), then Find would actually have a better performance advantange when trying to querying again?
Yes it will have better performance