简体   繁体   中英

Implementing DbContextOptions

In the following code, I don't understand why we addressed ApplicationDbContext after DbContextOptions

namespace SportsStore.Models
{
    public class ApplicationDbContext : DbContext
    {
        public ApplicationDbContext(DbContextOptions<ApplicationDbContext>options)
            : base(options)
        {
        }   

        public DbSet<Product> Products { get; set; }
    }
}

the definition of base class construction is :

 public DbContext([NotNullAttribute] DbContextOptions options);

Is ApplicationDbContext in above code is an attribute here? or is it a generic type and why we should use a generic type here? because when it comes to implementing the construction in the official documentation it is mentioned:

The option to be used by a DbContext . You normally override DbContext.OnConfiguring(DbContextOptionBuilder) or use a DbContextOptionBuilder to create instances of this class ...

and if we want to configure the derived class construction according to the official Microsoft documentation how we should proceed?

This is so that you can use Dependency Injection to instantiate your context, namely, through the AddDbContext extension method. In OnConfiguring you can check if the IsConfigured property to see if the context was configured through the constructor (the options parameter) or not. DbContextOptions<T> merely extends DbContextOptions and brings nothing new, but this way you can see that it is tied to a specific DbContext . See https://docs.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.dbcontext.onconfiguring?view=efcore-2.0 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM