using System; using System.IO; namespace ColdMint.scripts.database; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; /// /// Game database manager /// 游戏数据库管理器 /// public class DataBaseManager { private static ServiceProvider? _serviceProvider; /// /// Initialize database /// 初始化数据库 /// public static void InitDataBases(string databasePath) { var serviceCollection = new ServiceCollection(); serviceCollection.AddDbContext(options => options.UseSqlite($"Data Source={Path.Join(databasePath, Config.SolutionName + ".db")}")); _serviceProvider = serviceCollection.BuildServiceProvider(); var dataPackDbContext = GetRequiredService(); dataPackDbContext.Database.EnsureCreated(); } /// /// Get database service /// 获取数据库服务 /// /// /// public static T GetRequiredService() where T : notnull { if (_serviceProvider == null) { throw new NullReferenceException("service provider is null"); } var scope = _serviceProvider.CreateScope(); return scope.ServiceProvider.GetRequiredService(); } }