I have dbcontext class.
public class ApplicationDbContext : DbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } public DbSet<VH> Vhs { get; set; } }
Now I want to use it in controller. I have to pass an argument to the constructor.
[HttpGet("{id}")] public string Get(string id) { return GetBlockId(id); } private string GetBlockId(string btn) { var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); using (ApplicationDbContext context = new ApplicationDbContext(optionsBuilder.Options)) { } }
However I already define
optionsBuilder
in Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddDbContext<ApplicationDbContext>(options=>options.UseSqlServer("Data Source=something;Initial Catalog=Master;User ID=sa;password=xxxx;Application Name=Validation")); services.AddMvc(); }