Home

How do I connect LinqPad to an EF Core model when the DbContext has extra constructor parameters?

I have an EF Core model, and have been using that successfully from LinqPad for a while. The context class is fairly standard, with an additional constructor and configuring method added to ease usage with LinqPad...

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

  protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
    if (AppDomain.CurrentDomain.FriendlyName.StartsWith("LINQPad")) {
      optionsBuilder.UseLazyLoadingProxies();
    }
  }

  protected AppDbContext(DbContextOptions options) : base(options) {
  }

I now have the need to add an additional parameter to the first constructor...

  public AppDbContext(DbContextOptions<AppDbContext> options, string tenantId) : base(options) {
  }

However, this gives an exception when I try to query from LinqPad, as LinqPad is (presumably) expecting a constructor with only the options parameter, and so doesn't know what to supply for the string.

I can get around that by keeping the original constructor as well...

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

  public AppDbContext(DbContextOptions<AppDbContext> options, string tenantId) : base(options) {
  }

The problem is that if I do this, then I can't run the code, as I get a runtime exception of...

InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'MyProject.AppDbContext'. There should only be one applicable constructor.

Anyone any idea what I can do to enable me to be able to use the EF Core model from LinqPad without it preventing me from running my code?

Thanks

Comments

Sign In or Register to comment.