AppSettingsJson.cs
1.15 KB
using System.IO;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using WebRepository;
namespace Infrastructure
{
public class AppSettingsJson
{
public static string ApplicationExeDirectory()
{
var location = System.Reflection.Assembly.GetExecutingAssembly().Location;
var appRoot = Path.GetDirectoryName(location);
return appRoot;
}
public static IConfigurationRoot GetAppSettings()
{
string applicationExeDirectory = ApplicationExeDirectory();
var builder = new ConfigurationBuilder()
.SetBasePath(applicationExeDirectory)
.AddJsonFile("appsettings.json");
return builder.Build();
}
public static BaseDBContext JobContext(string ConnString)
{
var config = AppSettingsJson.GetAppSettings();
var optionsBuilder = new DbContextOptionsBuilder<BaseDBContext>();
optionsBuilder.UseSqlServer(ConnString);
BaseDBContext dBContext = new BaseDBContext(optionsBuilder.Options, config);
return dBContext;
}
}
}