Disable Tracking
Code
- Products\Details.cshtml.cs
AsNoTracking()
- DbExtensions.AsNoTracking Method
- Returns a new query where the entities returned will not be cached in the DbContext or ObjectContext.
- This method works by calling the AsNoTracking method of the underlying query object.
Laoding Patterns
Code
- Customers\Details.cshtml.cs
- Startup.cs
- Models\Customer.cs
Database Context Pooling
- EntityFrameworkServiceCollectionExtensions.AddDbContextPool Method
- Registers the given DbContext as a service in the IServiceCollection, and enables DbContext pooling for this registration.
- DbContext pooling can increase performance in high-throughput scenarios by re-using context instances.
- Note that when using pooling, the context configuration cannot change between uses, and scoped services injected into the context will only be resolved once from the initial scope.
- Use this method when using dependency injection in your application, such as with ASP.NET Core.
Eager Loading
- Eager Loading
- Eager loading means that the related data is loaded from the database as part of the initial query.
- Eager Loading of Related Data
- You can use the
Include
method to specify related data to be included in query results.
- You can use the
Lazy Loading
- Lazy Loading
- Lazy loading means that the related data is transparently loaded from the database when the navigation property is accessed.
- Lazy loading with proxies
- The simplest way to use lazy-loading is by installing the
Microsoft.EntityFrameworkCore.Proxies
package and enabling it with a call toUseLazyLoadingProxies
.
- The simplest way to use lazy-loading is by installing the
SQL Interpolatd
Code
- Pages\Products\Index.cshtml.cs
FromSqlInterpolatd
- RelationalQueryableExtensions.FromSqlInterpolated Method
- Creates a LINQ query based on an interpolated string representing a SQL query.
- If the database provider supports composing on the supplied SQL, you can compose on top of the raw SQL query using LINQ operators
- As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack.
- You can include interpolated parameter place holders in the SQL query string.
- Any interpolated parameter values you supply will automatically be converted to a DbParameter
Snapshot retreive
Code
- Pages\Products\Delete.cshtml.cs
FindAsync(Object[])
- DbSet.FindAsync Method
- Asynchronously 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.