Skip to content

Instantly share code, notes, and snippets.

@polatengin
Created February 24, 2018 12:49
Show Gist options
  • Save polatengin/464d78a39de20e32bd4c33f62b847bbf to your computer and use it in GitHub Desktop.
Save polatengin/464d78a39de20e32bd4c33f62b847bbf to your computer and use it in GitHub Desktop.
Asp.Net Core uygulamalarında Environment (Ortam) bilgisine göre Database ConnectionString (Veritabanı Bağlantı Cümlesi) belirlemek
public class AppDataContext : DbContext
{
public DbSet<Customer> Customers { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Employee> Employees { get; set; }
}
{
"ConnectionStrings": {
"AppConnectionString": "Data Source={SQLSERVERIP};Initial Catalog={DBNAME-PREFIX};User Id={USERNAME};Password={PASSWORD};"
}
}
public void ConfigureServices(IServiceCollection services)
{
services.AddDbContextPool<AppDataContext>(options =>
{
var builder = new SqlConnectionStringBuilder(Configuration.GetConnectionString("AppConnectionString"));
builder.InitialCatalog += "_" + HostingEnvironment.EnvironmentName;
if (HostingEnvironment.IsDevelopment())
{
builder.InitialCatalog += "_" + Environment.MachineName;
}
var connectionString = builder.ConnectionString;
options.UseSqlServer(connectionString);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment