-1

in my program.cs file i have the DB service setup as

builder.Services.AddDbContext<ADbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("ADevDB"));
});

and under the DatabaseConetxt class i have

public class ADbContext : DbContext
{
    public ADbContext(DbContextOptions<ADbContext> options) : base(options)
    {



    }
}

i am trying to access EFCore in a custom filter class without constructor injection. this is because when i apply the [CustomFilter] on an action i don't want it to request that an argument needs to be passed to the customFilter Constructor for DBConetext

rizu
  • 1,047
  • 1
  • 12
  • 21
  • 1
    IServiceProvider will create the instance for you: https://stackoverflow.com/questions/32459670/resolving-instances-with-asp-net-core-di-from-within-configureservices – GH DevOps May 17 '22 at 16:00
  • I'm not sure if this is what you're getting at, but possible duplicate of this: https://stackoverflow.com/questions/52724974/how-can-i-use-dependency-injection-in-a-net-core-actionfilterattribute – ProgrammingLlama May 17 '22 at 16:30
  • @DiplomacyNotWar its not what i am looking for – rizu May 17 '22 at 16:37
  • What kind of filter are we talking about here then? Can you edit your question to include an example? – ProgrammingLlama May 17 '22 at 23:26

1 Answers1

-1
var connectionstring = "Connection string";

var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
    optionsBuilder.UseSqlServer(connectionstring);


ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options);

// Or you can also instantiate inside using

using(ApplicationDbContext dbContext = new ApplicationDbContext(optionsBuilder.Options))
{
   //...do stuff
}

Check on this question too for various answers

MKougiouris
  • 2,821
  • 1
  • 16
  • 19
  • already using constructor injector on all controllers, doing the above will break that. the Idea is not to break the controller, but to find a way to use EF WITHIN the custom filter WITHOUT using constructor injector – rizu May 17 '22 at 20:30
  • what do you mean "without constructor injector?". The above does not use any injection at all, but an ef context does need some options to be instantiated. In the above example we only require the connection string... if you dont even have that what exactly do you expect? No injection and no connection string for the db? – MKougiouris May 18 '22 at 20:12
  • Please read the question again, you will see that the first snippet of code i added already shows how i use builder to set DB context, and i use it by injecting the ``` public class MyController { private readonly ApplicationDbContext _context; public MyController(ApplicationDbContext context) { _context = context; } } ``` The problem is that i am now trying to use EF in a class now "you can call this an helper class" and don't want to inject it through the construtor – rizu May 19 '22 at 11:30
  • But the way i am showing you does not inject it in the constructor.. you should look closer to what i showed you here. This is not a part of the startUp class, the only way to get a nicely builded dbContext, without injection, is to use the static "DbContextOptionsBuilder" from anywhere like shown above and consume the contexxt in a using... What you have on the original question is the booilerplate to setup DI.... – MKougiouris Jul 17 '22 at 17:01