Implementing DbContextOptions

asp.net-mvc c# entity-framework-core

Question

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?

1
1
4/10/2018 1:51:13 PM

Popular Answer

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.

0
4/10/2018 2:52:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow