|
| 1 | +using System; |
| 2 | +using System.Reflection; |
| 3 | +using Abp.Castle.Logging.Log4Net; |
| 4 | +using Abp.Dependency; |
| 5 | +using Abp.Domain.Uow; |
| 6 | +using Abp.EntityFrameworkCore; |
| 7 | +using Abp.EntityFrameworkCore.Configuration; |
| 8 | +using Abp.Modules; |
| 9 | +using Abp.MultiTenancy; |
| 10 | +using Abp.Zero.EntityFrameworkCore; |
| 11 | +using Castle.Facilities.Logging; |
| 12 | +using Castle.LoggingFacility.MsLogging; |
| 13 | +using Castle.Windsor.MsDependencyInjection; |
| 14 | +using Microsoft.EntityFrameworkCore; |
| 15 | +using Microsoft.Extensions.DependencyInjection; |
| 16 | +using ILoggerFactory = Castle.Core.Logging.ILoggerFactory; |
| 17 | + |
| 18 | +namespace Abp.Zero.SampleApp.EntityFrameworkCore.ConsoleAppTest |
| 19 | +{ |
| 20 | + public class Program |
| 21 | + { |
| 22 | + public static void Main(string[] args) |
| 23 | + { |
| 24 | + using (var abpBootstrapper = AbpBootstrapper.Create<EfCoreTestConsoleAppModule>()) |
| 25 | + { |
| 26 | + abpBootstrapper.IocManager.IocContainer.AddFacility<LoggingFacility>( |
| 27 | + f => f.UseAbpLog4Net().WithConfig("log4net.config") |
| 28 | + ); |
| 29 | + |
| 30 | + abpBootstrapper.Initialize(); |
| 31 | + abpBootstrapper.IocManager.Using<MigratorRunner>(migrateTester => migrateTester.Run()); |
| 32 | + } |
| 33 | + |
| 34 | + Console.WriteLine("Press Enter to EXIT!"); |
| 35 | + Console.ReadLine(); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + [DependsOn(typeof(SampleAppEntityFrameworkCoreModule))] |
| 40 | + public class EfCoreTestConsoleAppModule : AbpModule |
| 41 | + { |
| 42 | + public override void PreInitialize() |
| 43 | + { |
| 44 | + Configuration.DefaultNameOrConnectionString = "Server=localhost; Database=AbpZeroMigrateTest; Trusted_Connection=True;"; |
| 45 | + |
| 46 | + var services = new ServiceCollection(); |
| 47 | + services.AddLogging(); |
| 48 | + services.AddEntityFrameworkSqlServer(); |
| 49 | + |
| 50 | + var serviceProvider = WindsorRegistrationHelper.CreateServiceProvider( |
| 51 | + IocManager.IocContainer, |
| 52 | + services |
| 53 | + ); |
| 54 | + |
| 55 | + var castleLoggerFactory = serviceProvider.GetService<ILoggerFactory>(); |
| 56 | + if (castleLoggerFactory != null) |
| 57 | + { |
| 58 | + serviceProvider |
| 59 | + .GetRequiredService<Microsoft.Extensions.Logging.ILoggerFactory>() |
| 60 | + .AddCastleLogger(castleLoggerFactory); |
| 61 | + } |
| 62 | + |
| 63 | + Configuration.Modules.AbpEfCore().AddDbContext<AppDbContext>(configuration => |
| 64 | + { |
| 65 | + configuration.DbContextOptions |
| 66 | + .UseInternalServiceProvider(serviceProvider) |
| 67 | + .UseSqlServer(configuration.ConnectionString); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public override void Initialize() |
| 72 | + { |
| 73 | + IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly()); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public class MigratorRunner : ITransientDependency |
| 78 | + { |
| 79 | + private readonly AppTestMigrator _appTestMigrator; |
| 80 | + |
| 81 | + public MigratorRunner(AppTestMigrator appTestMigrator) |
| 82 | + { |
| 83 | + _appTestMigrator = appTestMigrator; |
| 84 | + } |
| 85 | + |
| 86 | + public void Run() |
| 87 | + { |
| 88 | + _appTestMigrator.CreateOrMigrateForHost(); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public class AppTestMigrator : AbpZeroDbMigrator<AppDbContext> |
| 93 | + { |
| 94 | + public AppTestMigrator( |
| 95 | + IUnitOfWorkManager unitOfWorkManager, |
| 96 | + IDbPerTenantConnectionStringResolver connectionStringResolver, |
| 97 | + IIocResolver iocResolver, |
| 98 | + IDbContextResolver dbContextResolver) |
| 99 | + : base(unitOfWorkManager, |
| 100 | + connectionStringResolver, |
| 101 | + iocResolver, |
| 102 | + dbContextResolver) |
| 103 | + { |
| 104 | + |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments