/// <summary> /// Register entity events /// </summary> /// <param name="services"></param> /// <returns></returns> public static IServiceCollection AddEntityModuleEvents(this IServiceCollection services) { //Register entity events EntityEvents.RegisterEvents(); SystemEvents.Application.OnApplicationStarted += delegate(object sender, ApplicationStartedEventArgs args) { var scopeContextFactory = (DbContext)args.Services.GetRequiredService <IEntityContext>(); DbConnectionFactory.Connection.SetConnection(scopeContextFactory.Database.GetDbConnection()); }; SystemEvents.Application.OnApplicationStopped += delegate { DbConnectionFactory.CloseAll(); }; return(services); }