8
8
using DevExpress . ExpressApp . Layout ;
9
9
using DevExpress . ExpressApp . Security ;
10
10
using DevExpress . ExpressApp . SystemModule ;
11
+ using Microsoft . Extensions . Configuration ;
11
12
using Microsoft . Extensions . DependencyInjection ;
12
13
13
14
namespace XAF . Testing . XAF {
@@ -78,6 +79,7 @@ public static IObservable<Frame> Navigate(this XafApplication application,string
78
79
79
80
public static bool TenantsExist ( this XafApplication application , string connectionString = null , int recordCount = 2 ) {
80
81
connectionString ??= application . ConnectionString ;
82
+ if ( ! application . DbExist ( connectionString ) ) return false ;
81
83
using var sqlConnection = new SqlConnection ( connectionString ) ;
82
84
var cmdText = @"
83
85
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Tenant')
@@ -95,8 +97,21 @@ IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'dbo' AN
95
97
var result = command . ExecuteScalar ( ) ! ;
96
98
sqlConnection . Close ( ) ;
97
99
return result == ( object ) 1 ;
100
+
98
101
}
99
-
102
+
103
+ public static void DropDb ( this XafApplication application , string connectionString = null ) {
104
+ if ( ! application . DbExist ( connectionString ) ) return ;
105
+ var builder = new SqlConnectionStringBuilder ( connectionString ?? application . ConnectionString ) ;
106
+ var initialCatalog = "Initial catalog" ;
107
+ var databaseName = builder [ initialCatalog ] . ToString ( ) ;
108
+ builder . Remove ( initialCatalog ) ;
109
+ using SqlConnection connection = new SqlConnection ( builder . ConnectionString ) ;
110
+ connection . Open ( ) ;
111
+ using SqlCommand cmd = new SqlCommand ( $ "USE master; ALTER DATABASE [{ databaseName } ] SET SINGLE_USER WITH ROLLBACK IMMEDIATE; DROP DATABASE [{ databaseName } ];", connection ) ;
112
+ cmd . ExecuteNonQuery ( ) ;
113
+ }
114
+
100
115
public static bool DbExist ( this XafApplication application , string connectionString = null ) {
101
116
var builder = new SqlConnectionStringBuilder ( connectionString ?? application . ConnectionString ) ;
102
117
var initialCatalog = "Initial catalog" ;
@@ -152,8 +167,7 @@ public static IObservable<Window> WhenWindowCreated(this XafApplication applicat
152
167
}
153
168
154
169
private static IObservable < Window > WhenMainWindowAvailable ( this IObservable < Window > windowCreated )
155
- => windowCreated . When ( TemplateContext . ApplicationWindow ) . Select ( window => window ) . TemplateChanged ( ) . Cast < Window > ( ) . Take ( 1 )
156
- . Select ( window => window ) ;
170
+ => windowCreated . When ( TemplateContext . ApplicationWindow ) . TemplateChanged ( ) . Cast < Window > ( ) . Take ( 1 ) ;
157
171
158
172
public static IObservable < Frame > Navigate ( this Window window , string viewId , IObservable < Frame > afterNavigation , Func < Window , IObservable < Unit > > navigate = null ) {
159
173
navigate ??= _ => Unit . Default . Observe ( ) ;
0 commit comments