I know that to use EF Core in a class library a startup project is needed, as explained here: Is EF Core Add Migration Supported from .NET Standard Library?
However, I've managed to use dotnet ef migrations bundle
locally using a single class library project (without using a startup project) by
- Adding a ConfigurationBuilder you dbcontext
- Overriding the "OnConfiguring" method to use the ConfigurationBuilder for reading the connection string where you have it. In my case from user secrets
public partial class MyContext : DbContext
{
private readonly IConfiguration configuration;
//will take the connection string from user secrets
public MyContext()
{
this.configuration = new ConfigurationBuilder()
.AddUserSecrets<ClpQUkMiContext>()
.Build();
}
public MyContext(IConfiguration configuration)
{
this.configuration = configuration;
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
var connectionString = configuration["ConnectionStringInUserSecret"]
optionsBuilder.UseSqlServer(connectionString);
}
}
However, when I'm trying to use it in azure pipeline it's failing to find the connection string, which I have stored in a secret variable. So far I've tried
- Adding
".AddEnvironmentVariables()"
to the configuration builder. - Mapping the secret variable in the yaml file, like
variables:
MyConnectionString: $(MySecreatConnectionString) # Setting variable to be read by the context
I would appreciate any help.