Reputation: 9472
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
Upvotes: 1
Views: 55
Reputation: 30964
From LINQPad v8.4.11, you can add extra parameters to the constructor that accepts a DbContextOptions<AppDbContext>
:
public AppDbContext(DbContextOptions<AppDbContext> options, string tenantId = null)
{
...
Note that any extra parameters must be optional (i.e., have a default value) so that LINQPad can still call them.
Upvotes: 1