コード例 #1
0
        /// <summary>
        /// 创建一个数据上下文实例
        /// </summary>
        /// <param name="args">参数</param>
        /// <returns></returns>
        public virtual TDbContext CreateDbContext(string[] args)
        {
            string migrationAssemblyName = GetType().Assembly.GetName().Name;

            ServiceExtensions.MigrationAssemblyName = migrationAssemblyName;
            Console.WriteLine($@"MigrationAssembly: {migrationAssemblyName}");

            ServiceProvider = ServiceProvider ?? CreateDesignTimeServiceProvider();

            IEntityManager entityManager = ServiceProvider.GetService <IEntityManager>();

            entityManager.Initialize();

            DbContextOptionsBuilder builder = new DbContextOptionsBuilder <TDbContext>();

            builder = ServiceProvider.BuildDbContextOptionsBuilder <TDbContext>(builder);

            TDbContext context = (TDbContext)ActivatorUtilities.CreateInstance(ServiceProvider, typeof(TDbContext), builder.Options);

            return(context);
        }