I am struggling with this coming back from a long layoff.
I asked a question regarding the configuring of a DBContext in my generic base repository. Only after a user has logged in can I then construct a connection string so I cannot register a service in startup.cs - I have to use a constructor argument to instantiate my DBContext.
I got this answer which I thought would address the problem however I am getting an error in the following factory class:
public class ContextFactory<T> : IContextFactory<T> : where T : DbContext
{
public T CreateDbContext(string connectionString)
{
var optionsBuilder = new DbContextOptionsBuilder<T>();
optionsBuilder.UseSqlServer(connectionString);
return new T(optionsBuilder.Options);
}
}
The error is on the line return new T(optionsBuilder.Options); and is:
Cannot create an instance of the variable type 'T' because it does not have the new() constraint
new()constraint you end up with'T': cannot provide arguments when creating an instance of a variable type. You were given invalid code.