Was geht in DbContextOptions, wenn ein neuer DbContext aufgerufen wird?

Ich verwende DI nicht und möchte einfach einen DbContext in meinem Controller aufrufen. Ich habe Mühe, herauszufinden, was die "Optionen" sein sollten?

ApplicationDbContext.cs

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

    public DbSet<Gig> Gigs { get; set; }
    public DbSet<Genre> Genres { get; set; }


    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

GigsController.cs

    public class GigsController : Controller
{
    private ApplicationDbContext _context;

    public GigsController()
    {
        _context = new ApplicationDbContext();
    }


    public IActionResult Create()
    {
        var viewModel = new GigFormViewModel
        {
            Genres = _context.Genres.ToList()
        };


        return View(viewModel);
    }
}

Das Problem tritt in meinem GigsController-Konstruktor auf:

_context = new ApplicationDbContext();

Ich mache einen Fehler, weil ich etwas an ApplicationDbContext übergeben muss. Es wurde kein Argument angegeben, das dem erforderlichen formalen Parameter 'options' von 'ApplicationDbContext.ApplicationDbContext (DbContextOptions)' entspricht.

Ich habe versucht, einen Standardkonstruktor in ApplicationDbContext zu erstellen, der von base () abgeleitet ist, aber das hat auch nicht funktioniert.

In meiner startup.cs habe ich den ApplicationDbContext @ konfigurie

        public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        services.AddMvc();

        // Add application services.
        services.AddTransient<IEmailSender, AuthMessageSender>();
        services.AddTransient<ISmsSender, AuthMessageSender>();
    }

Antworten auf die Frage(4)

Ihre Antwort auf die Frage