A session with the database that can be used to query and save instances of your entities
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. 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. See Avoiding DbContext threading issues for more information and examples.
Typically you create a class that derives from DbContext and contains DbSet<TEntity>
properties for each entity in the model. If the DbSet<TEntity>
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 you would rather perform configuration externally instead of inline in your context, you can use DbContextOptionsBuilder<TContext>
(or DbContextOptionsBuilder
) to externally create an instance of DbContextOptions<TContext>
(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<TEntity>
properties on the derived context. To further configure the model that is discovered by convention, you can override the OnModelCreating(ModelBuilder)
method.
The lifetime of a DbContext begins when the instance is created and ends when the instance is disposed. A DbContext instance is designed to be used for a single unit-of-work. This means that the lifetime of a DbContext instance is usually very short.
A typical unit-of-work when using Entity Framework Core (EF Core) involves:
SaveChanges
or SaveChangesAsync
is called. EF Core detects the changes made and writes them to the database.It is very important to dispose the DbContext after use. This ensures both that any unmanaged resources are freed, and that any events or other hooks are unregistered so as to prevent memory leaks in case the instance remains referenced.
InvalidOperationException
thrown by EF Core code can put the context into an unrecoverable state. Such exceptions indicate a program error and are not designed to be recovered from.Quando si crea un DBContext specifico per un determinato dominio applicativo, si crea una classe che discende dalla classe DbContext
. In questa classe, sono molto importanti i metodi:
OnConfiguring(DbContextOptionsBuilder)
Override this method to configure the database (and other options) to be used for this context. This method is called for each instance of the context that is created. The base implementation does nothing.
Questo metodo, di solito, contiene informazioni importanti che servono al DBContext per sapere dove si trova il Data Source, quali sono i parametri di connessione, etc.
OnModelCreating(ModelBuilder)
Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet<TEntity>
properties on your derived context. The resulting model may be cached and re-used for subsequent instances of your derived context.
Questo metodo viene usato per completare la definizione del modello dei dati, aggiungendo vincoli e parametri che non sono ricavati automaticamente da EF Core mediante l’ispezione delle classi del modello dei dati.