I'm using .net core 2.0 and my structure is like that basically: I have 20 Web Api project and one class library (DbContext) project. Web Api projects have Startup.cs. But class library hasn't got Startup.cs. Where should I write the connection string? Because class library has no app.config or Startup.cs. If I write in Startup.cs of API's, then I have to write connectionString 20 times. This is not acceptable. I wrote using DbContextOptions like below but I'm not sure, it's the best way. What is the best way for this?
public class MyDbContext : DbContext
{
private readonly IAuditHelper auditHelper;
public MyDbContext(DbContextOptions<MyDbContext> options, IAuditHelper auditHelper)
: base(GetOptions())
{
this.auditHelper = auditHelper;
}
private static DbContextOptions GetOptions()
{
return SqlServerDbContextOptionsExtensions.UseSqlServer(new DbContextOptionsBuilder(), "server=asdf; database=asdf; user id=asdf; password=asdf").Options;
}
public async Task<int> SaveChangesWithoutAuditAsync(CancellationToken cancellationToken = default(CancellationToken))
{
return (await base.SaveChangesAsync(true, cancellationToken));
}
}