I am moving away from using SQL Authentication with my Azure DB, to Active Directory Managed Authentication as explained in this article.
Basically, I'm doing two main things to get this working.
1- injecting the token in the DBContext constructor:
public MyDBContext(DbContextOptions<MyDBContext> options)
: base(options)
{
var conn = (SqlConnection)Database.GetDbConnection();
conn.AccessToken = (new Microsoft.Azure.Services.AppAuthentication.AzureServiceTokenProvider()).GetAccessTokenAsync("https://database.windows.net/").Result;
}
2- In My Web App Startup file, I'm injecting the DBContext
string SqlConnection = localConfig["SqlConnectionString"];
services.AddDbContext<MyDBContext>(options => options.UseSqlServer(SqlConnection, sqlServerOptions => { sqlServerOptions.CommandTimeout(1000); }));
My problem now is that every time I need to refresh the model using the Scaffold-DbContext command, my MyDbContext gets overwritten, and I lose the changes I've done to the constructor.
What solutions are possible to avoid this problem? OR, is there a better way to inject the Token somewhere else efficiently?
Edit: Please note that I am using EF 2.x