anitsh / til

Today I Learn (til) - Github `Issues` used as daily learning management system for taking notes and storing resource links.
https://anitshrestha.com.np
MIT License
76 stars 11 forks source link

DbContext Class - Entity Framework #1021

Open anitsh opened 1 year ago

anitsh commented 1 year ago

DbContext Class

A DbContext instance represents a session with the database and can be used to query and save instances of your entities. DbContext is a combination of the Unit Of Work and Repository patterns.

public class DbContext : IAsyncDisposable, IDisposable, Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies, Microsoft.EntityFrameworkCore.Internal.IDbContextPoolable, Microsoft.EntityFrameworkCore.Internal.IDbSetCache

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel.

Typically we create a class that derives from DbContext and contains DbSet properties for each entity in the model. If the DbSet properties have a public setter, they are automatically initialized when the instance of the derived context is created.

Override the OnConfiguring(DbContextOptionsBuilder) method to configure the database (and other options) to be used for the context. Alternatively, if we would rather perform configuration externally instead of inline in our context, we can use DbContextOptionsBuilder (or DbContextOptionsBuilder) to externally create an instance of DbContextOptions (or DbContextOptions) and pass it to a base constructor of DbContext.

The model is discovered by running a set of conventions over the entity classes found in the DbSet properties on the derived context. To further configure the model that is discovered by convention, we can override the OnModelCreating(ModelBuilder) method.

https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext?view=efcore-7.0

anitsh commented 1 year ago

EntityState

An event fired when an entity that is tracked by the associated DbContext has moved from one EntityState to another.

EntityState Enum

The state in which an entity is being tracked by a context.

Added 4 The entity is being tracked by the context but does not yet exist in the database.
Deleted 2 The entity is being tracked by the context and exists in the database. It has been marked for deletion from the database.
Detached 0 The entity is not being tracked by the context.
Modified 3 The entity is being tracked by the context and exists in the database. Some or all of its property values have been modified.
Unchanged 1 The entity is being tracked by the context and exists in the database. Its property values have not changed from the values in the database.

ChangeTracker.Clear Method

DbContext is designed to have a short lifetime where a new instance is created for each unit-of-work. This manner means all tracked entities are discarded when the context is disposed at the end of each unit-of-work. However, clearing all tracked entities using this method may be useful in situations where creating a new context instance is not practical.

This method should always be preferred over detaching every tracked entity. Detaching entities is a slow process that may have side effects. This method is much more efficient at clearing all tracked entities from the context.

Note that this method does not generate StateChanged events since entities are not individually detached.