-
Notifications
You must be signed in to change notification settings - Fork 536
/
EntityFramework6.psm1
1209 lines (988 loc) · 38.5 KB
/
EntityFramework6.psm1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright (c) Microsoft Corporation. All rights reserved.
$ErrorActionPreference = 'Stop'
$InitialDatabase = '0'
<#
.SYNOPSIS
Adds or updates an Entity Framework provider entry in the project config
file.
.DESCRIPTION
Adds an entry into the 'entityFramework' section of the project config
file for the specified provider invariant name and provider type. If an
entry for the given invariant name already exists, then that entry is
updated with the given type name, unless the given type name already
matches, in which case no action is taken. The 'entityFramework'
section is added if it does not exist. The config file is automatically
saved if and only if a change was made.
This command is typically used only by Entity Framework provider NuGet
packages and is run from the 'install.ps1' script.
.PARAMETER Project
The Visual Studio project to update. When running in the NuGet install.ps1
script the '$project' variable provided as part of that script should be
used.
.PARAMETER InvariantName
The provider invariant name that uniquely identifies this provider. For
example, the Microsoft SQL Server provider is registered with the invariant
name 'System.Data.SqlClient'.
.PARAMETER TypeName
The assembly-qualified type name of the provider-specific type that
inherits from 'System.Data.Entity.Core.Common.DbProviderServices'. For
example, for the Microsoft SQL Server provider, this type is
'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer'.
#>
function Add-EFProvider
{
[CmdletBinding(PositionalBinding = $false)]
param(
[parameter(Position = 0, Mandatory = $true)]
$Project,
[parameter(Position = 1, Mandatory = $true)]
[string] $InvariantName,
[parameter(Position = 2, Mandatory = $true)]
[string] $TypeName)
$configPath = GetConfigPath $Project
if (!$configPath)
{
return
}
[xml] $configXml = Get-Content $configPath
$providers = $configXml.configuration.entityFramework.providers
$providers.provider |
where invariantName -eq $InvariantName |
%{ $providers.RemoveChild($_) | Out-Null }
$provider = $providers.AppendChild($configXml.CreateElement('provider'))
$provider.SetAttribute('invariantName', $InvariantName)
$provider.SetAttribute('type', $TypeName)
$configXml.Save($configPath)
}
<#
.SYNOPSIS
Adds or updates an Entity Framework default connection factory in the
project config file.
.DESCRIPTION
Adds an entry into the 'entityFramework' section of the project config
file for the connection factory that Entity Framework will use by default
when creating new connections by convention. Any existing entry will be
overridden if it does not match. The 'entityFramework' section is added if
it does not exist. The config file is automatically saved if and only if
a change was made.
This command is typically used only by Entity Framework provider NuGet
packages and is run from the 'install.ps1' script.
.PARAMETER Project
The Visual Studio project to update. When running in the NuGet install.ps1
script the '$project' variable provided as part of that script should be
used.
.PARAMETER TypeName
The assembly-qualified type name of the connection factory type that
implements the 'System.Data.Entity.Infrastructure.IDbConnectionFactory'
interface. For example, for the Microsoft SQL Server Express provider
connection factory, this type is
'System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework'.
.PARAMETER ConstructorArguments
An optional array of strings that will be passed as arguments to the
connection factory type constructor.
#>
function Add-EFDefaultConnectionFactory
{
[CmdletBinding(PositionalBinding = $false)]
param(
[parameter(Position = 0, Mandatory = $true)]
$Project,
[parameter(Position = 1, Mandatory = $true)]
[string] $TypeName,
[string[]] $ConstructorArguments)
$configPath = GetConfigPath $Project
if (!$configPath)
{
return
}
[xml] $configXml = Get-Content $configPath
$entityFramework = $configXml.configuration.entityFramework
$defaultConnectionFactory = $entityFramework.defaultConnectionFactory
if ($defaultConnectionFactory)
{
$entityFramework.RemoveChild($defaultConnectionFactory) | Out-Null
}
$defaultConnectionFactory = $entityFramework.AppendChild($configXml.CreateElement('defaultConnectionFactory'))
$defaultConnectionFactory.SetAttribute('type', $TypeName)
if ($ConstructorArguments)
{
$parameters = $defaultConnectionFactory.AppendChild($configXml.CreateElement('parameters'))
foreach ($constructorArgument in $ConstructorArguments)
{
$parameter = $parameters.AppendChild($configXml.CreateElement('parameter'))
$parameter.SetAttribute('value', $constructorArgument)
}
}
$configXml.Save($configPath)
}
<#
.SYNOPSIS
Enables Code First Migrations in a project.
.DESCRIPTION
Enables Migrations by scaffolding a migrations configuration class in the project. If the
target database was created by an initializer, an initial migration will be created (unless
automatic migrations are enabled via the EnableAutomaticMigrations parameter).
.PARAMETER ContextTypeName
Specifies the context to use. If omitted, migrations will attempt to locate a
single context type in the target project.
.PARAMETER EnableAutomaticMigrations
Specifies whether automatic migrations will be enabled in the scaffolded migrations configuration.
If omitted, automatic migrations will be disabled.
.PARAMETER MigrationsDirectory
Specifies the name of the directory that will contain migrations code files.
If omitted, the directory will be named "Migrations".
.PARAMETER ProjectName
Specifies the project that the scaffolded migrations configuration class will
be added to. If omitted, the default project selected in package manager
console is used.
.PARAMETER StartUpProjectName
Specifies the configuration file to use for named connection strings. If
omitted, the specified project's configuration file is used.
.PARAMETER ContextProjectName
Specifies the project which contains the DbContext class to use. If omitted,
the context is assumed to be in the same project used for migrations.
.PARAMETER ConnectionStringName
Specifies the name of a connection string to use from the application's
configuration file.
.PARAMETER ConnectionString
Specifies the connection string to use. If omitted, the context's
default connection will be used.
.PARAMETER ConnectionProviderName
Specifies the provider invariant name of the connection string.
.PARAMETER Force
Specifies that the migrations configuration be overwritten when running more
than once for a given project.
.PARAMETER ContextAssemblyName
Specifies the name of the assembly which contains the DbContext class to use. Use this
parameter instead of ContextProjectName when the context is contained in a referenced
assembly rather than in a project of the solution.
.PARAMETER AppDomainBaseDirectory
Specifies the directory to use for the app-domain that is used for running Migrations
code such that the app-domain is able to find all required assemblies. This is an
advanced option that should only be needed if the solution contains several projects
such that the assemblies needed for the context and configuration are not all
referenced from either the project containing the context or the project containing
the migrations.
.EXAMPLE
Enable-Migrations
# Scaffold a migrations configuration in a project with only one context
.EXAMPLE
Enable-Migrations -Auto
# Scaffold a migrations configuration with automatic migrations enabled for a project
# with only one context
.EXAMPLE
Enable-Migrations -ContextTypeName MyContext -MigrationsDirectory DirectoryName
# Scaffold a migrations configuration for a project with multiple contexts
# This scaffolds a migrations configuration for MyContext and will put the configuration
# and subsequent configurations in a new directory called "DirectoryName"
#>
function Enable-Migrations
{
[CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
param(
[string] $ContextTypeName,
[alias('Auto')]
[switch] $EnableAutomaticMigrations,
[string] $MigrationsDirectory,
[string] $ProjectName,
[string] $StartUpProjectName,
[string] $ContextProjectName,
[parameter(ParameterSetName = 'ConnectionStringName')]
[string] $ConnectionStringName,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionString,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionProviderName,
[switch] $Force,
[string] $ContextAssemblyName,
[string] $AppDomainBaseDirectory)
WarnIfOtherEFs 'Enable-Migrations'
$project = GetProject $ProjectName
$startupProject = GetStartupProject $StartUpProjectName $project
if (!$ContextAssemblyName -and $ContextProjectName)
{
$contextProject = Get-Project $ContextProjectName
$ContextAssemblyName = GetProperty $contextProject.Properties 'AssemblyName'
}
$params = 'migrations', 'enable', '--json'
if ($ContextTypeName)
{
$params += '--context', $ContextTypeName
}
if ($ContextAssemblyName)
{
$params += '--context-assembly', $ContextAssemblyName
}
if ($EnableAutomaticMigrations)
{
$params += '--auto'
}
if ($MigrationsDirectory)
{
$params += '--migrations-dir', $MigrationsDirectory
}
$params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
if ($Force)
{
$params += '--force'
}
# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
$result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n" | ConvertFrom-Json
$project.ProjectItems.AddFromFile($result.migrationsConfiguration) | Out-Null
$DTE.ItemOperations.OpenFile($result.migrationsConfiguration) | Out-Null
ShowConsole
if ($result.migration)
{
$project.ProjectItems.AddFromFile($result.migration) | Out-Null
$resourcesProperties = $project.ProjectItems.AddFromFile($result.migrationResources).Properties
$project.ProjectItems.AddFromFile($result.migrationDesigner) | Out-Null
}
}
<#
.SYNOPSIS
Scaffolds a migration script for any pending model changes.
.DESCRIPTION
Scaffolds a new migration script and adds it to the project.
.PARAMETER Name
Specifies the name of the custom script.
.PARAMETER Force
Specifies that the migration user code be overwritten when re-scaffolding an
existing migration.
.PARAMETER ProjectName
Specifies the project that contains the migration configuration type to be
used. If omitted, the default project selected in package manager console
is used.
.PARAMETER StartUpProjectName
Specifies the configuration file to use for named connection strings. If
omitted, the specified project's configuration file is used.
.PARAMETER ConfigurationTypeName
Specifies the migrations configuration to use. If omitted, migrations will
attempt to locate a single migrations configuration type in the target
project.
.PARAMETER ConnectionStringName
Specifies the name of a connection string to use from the application's
configuration file.
.PARAMETER ConnectionString
Specifies the connection string to use. If omitted, the context's
default connection will be used.
.PARAMETER ConnectionProviderName
Specifies the provider invariant name of the connection string.
.PARAMETER IgnoreChanges
Scaffolds an empty migration ignoring any pending changes detected in the current model.
This can be used to create an initial, empty migration to enable Migrations for an existing
database. N.B. Doing this assumes that the target database schema is compatible with the
current model.
.PARAMETER AppDomainBaseDirectory
Specifies the directory to use for the app-domain that is used for running Migrations
code such that the app-domain is able to find all required assemblies. This is an
advanced option that should only be needed if the solution contains several projects
such that the assemblies needed for the context and configuration are not all
referenced from either the project containing the context or the project containing
the migrations.
.EXAMPLE
Add-Migration First
# Scaffold a new migration named "First"
.EXAMPLE
Add-Migration First -IgnoreChanges
# Scaffold an empty migration ignoring any pending changes detected in the current model.
# This can be used to create an initial, empty migration to enable Migrations for an existing
# database. N.B. Doing this assumes that the target database schema is compatible with the
# current model.
#>
function Add-Migration
{
[CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
param(
[parameter(Position = 0, Mandatory = $true)]
[string] $Name,
[switch] $Force,
[string] $ProjectName,
[string] $StartUpProjectName,
[string] $ConfigurationTypeName,
[parameter(ParameterSetName = 'ConnectionStringName')]
[string] $ConnectionStringName,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionString,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionProviderName,
[switch] $IgnoreChanges,
[string] $AppDomainBaseDirectory)
WarnIfOtherEFs 'Add-Migration'
$project = GetProject $ProjectName
$startupProject = GetStartupProject $StartUpProjectName $project
$params = 'migrations', 'add', $Name, '--json'
if ($Force)
{
$params += '--force'
}
if ($ConfigurationTypeName)
{
$params += '--migrations-config', $ConfigurationTypeName
}
if ($IgnoreChanges)
{
$params += '--ignore-changes'
}
$params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
# NB: -join is here to support ConvertFrom-Json on PowerShell 3.0
$result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n" | ConvertFrom-Json
$project.ProjectItems.AddFromFile($result.migration) | Out-Null
$DTE.ItemOperations.OpenFile($result.migration) | Out-Null
$resourcesProperties = $project.ProjectItems.AddFromFile($result.migrationResources).Properties
$project.ProjectItems.AddFromFile($result.migrationDesigner) | Out-Null
}
<#
.SYNOPSIS
Applies any pending migrations to the database.
.DESCRIPTION
Updates the database to the current model by applying pending migrations.
.PARAMETER SourceMigration
Only valid with -Script. Specifies the name of a particular migration to use
as the update's starting point. If omitted, the last applied migration in
the database will be used.
.PARAMETER TargetMigration
Specifies the name of a particular migration to update the database to. If
omitted, the current model will be used.
.PARAMETER Script
Generate a SQL script rather than executing the pending changes directly.
.PARAMETER Force
Specifies that data loss is acceptable during automatic migration of the
database.
.PARAMETER ProjectName
Specifies the project that contains the migration configuration type to be
used. If omitted, the default project selected in package manager console
is used.
.PARAMETER StartUpProjectName
Specifies the configuration file to use for named connection strings. If
omitted, the specified project's configuration file is used.
.PARAMETER ConfigurationTypeName
Specifies the migrations configuration to use. If omitted, migrations will
attempt to locate a single migrations configuration type in the target
project.
.PARAMETER ConnectionStringName
Specifies the name of a connection string to use from the application's
configuration file.
.PARAMETER ConnectionString
Specifies the connection string to use. If omitted, the context's
default connection will be used.
.PARAMETER ConnectionProviderName
Specifies the provider invariant name of the connection string.
.PARAMETER AppDomainBaseDirectory
Specifies the directory to use for the app-domain that is used for running Migrations
code such that the app-domain is able to find all required assemblies. This is an
advanced option that should only be needed if the solution contains several projects
such that the assemblies needed for the context and configuration are not all
referenced from either the project containing the context or the project containing
the migrations.
.EXAMPLE
Update-Database
# Update the database to the latest migration
.EXAMPLE
Update-Database -TargetMigration Second
# Update database to a migration named "Second"
# This will apply migrations if the target hasn't been applied or roll back migrations
# if it has
.EXAMPLE
Update-Database -Script
# Generate a script to update the database from its current state to the latest migration
.EXAMPLE
Update-Database -Script -SourceMigration Second -TargetMigration First
# Generate a script to migrate the database from a specified start migration
# named "Second" to a specified target migration named "First"
.EXAMPLE
Update-Database -Script -SourceMigration $InitialDatabase
# Generate a script that can upgrade a database currently at any version to the latest version.
# The generated script includes logic to check the __MigrationsHistory table and only apply changes
# that haven't been previously applied.
.EXAMPLE
Update-Database -TargetMigration $InitialDatabase
# Runs the Down method to roll-back any migrations that have been applied to the database
#>
function Update-Database
{
[CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
param(
[string] $SourceMigration,
[string] $TargetMigration,
[switch] $Script,
[switch] $Force,
[string] $ProjectName,
[string] $StartUpProjectName,
[string] $ConfigurationTypeName,
[parameter(ParameterSetName = 'ConnectionStringName')]
[string] $ConnectionStringName,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionString,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionProviderName,
[string] $AppDomainBaseDirectory)
WarnIfOtherEFs 'Update-Database'
$project = GetProject $ProjectName
$startupProject = GetStartupProject $StartUpProjectName $project
$params = 'database', 'update'
if ($SourceMigration)
{
$params += '--source', $SourceMigration
}
if ($TargetMigration)
{
$params += '--target', $TargetMigration
}
if ($Script)
{
$params += '--script'
}
if ($Force)
{
$params += '--force'
}
if ($ConfigurationTypeName)
{
$params += '--migrations-config', $ConfigurationTypeName
}
$params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
$result = (EF6 $project $startupProject $AppDomainBaseDirectory $params) -join "`n"
if ($result)
{
try
{
$window = $DTE.ItemOperations.NewFile('General\Sql File')
$textDocument = $window.Document.Object('TextDocument')
$editPoint = $textDocument.StartPoint.CreateEditPoint()
$editPoint.Insert($result)
}
catch
{
$intermediatePath = GetIntermediatePath $project
if (![IO.Path]::IsPathRooted($intermediatePath))
{
$projectDir = GetProperty $project.Properties 'FullPath'
$intermediatePath = Join-Path $projectDir $intermediatePath -Resolve | Convert-Path
}
$fileName = [IO.Path]::ChangeExtension([IO.Path]::GetRandomFileName(), '.sql')
$sqlFile = Join-Path $intermediatePath $fileName
[IO.File]::WriteAllText($sqlFile, $result)
$DTE.ItemOperations.OpenFile($sqlFile) | Out-Null
}
ShowConsole
}
}
<#
.SYNOPSIS
Displays the migrations that have been applied to the target database.
.DESCRIPTION
Displays the migrations that have been applied to the target database.
.PARAMETER ProjectName
Specifies the project that contains the migration configuration type to be
used. If omitted, the default project selected in package manager console
is used.
.PARAMETER StartUpProjectName
Specifies the configuration file to use for named connection strings. If
omitted, the specified project's configuration file is used.
.PARAMETER ConfigurationTypeName
Specifies the migrations configuration to use. If omitted, migrations will
attempt to locate a single migrations configuration type in the target
project.
.PARAMETER ConnectionStringName
Specifies the name of a connection string to use from the application's
configuration file.
.PARAMETER ConnectionString
Specifies the connection string to use. If omitted, the context's
default connection will be used.
.PARAMETER ConnectionProviderName
Specifies the provider invariant name of the connection string.
.PARAMETER AppDomainBaseDirectory
Specifies the directory to use for the app-domain that is used for running Migrations
code such that the app-domain is able to find all required assemblies. This is an
advanced option that should only be needed if the solution contains several projects
such that the assemblies needed for the context and configuration are not all
referenced from either the project containing the context or the project containing
the migrations.
#>
function Get-Migrations
{
[CmdletBinding(DefaultParameterSetName = 'ConnectionStringName', PositionalBinding = $false)]
param(
[string] $ProjectName,
[string] $StartUpProjectName,
[string] $ConfigurationTypeName,
[parameter(ParameterSetName = 'ConnectionStringName')]
[string] $ConnectionStringName,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionString,
[parameter(ParameterSetName = 'ConnectionStringAndProviderName', Mandatory = $true)]
[string] $ConnectionProviderName,
[string] $AppDomainBaseDirectory)
WarnIfOtherEFs 'Get-Migrations'
$project = GetProject $ProjectName
$startupProject = GetStartupProject $StartUpProjectName $project
$params = 'migrations', 'list'
if ($ConfigurationTypeName)
{
$params += '--migrations-config', $ConfigurationTypeName
}
$params += GetParams $ConnectionStringName $ConnectionString $ConnectionProviderName
return EF6 $project $startupProject $AppDomainBaseDirectory $params
}
function WarnIfOtherEFs($cmdlet)
{
if (Get-Module 'EntityFrameworkCore')
{
Write-Warning "Both Entity Framework 6 and Entity Framework Core are installed. The Entity Framework 6 tools are running. Use 'EntityFrameworkCore\$cmdlet' for Entity Framework Core."
}
if (Get-Module 'EntityFramework')
{
Write-Warning "A version of Entity Framework older than 6.3 is also installed. The newer tools are running. Use 'EntityFramework\$cmdlet' for the older version."
}
}
function GetProject($projectName)
{
if (!$projectName)
{
return Get-Project
}
return Get-Project $projectName
}
function GetStartupProject($name, $fallbackProject)
{
if ($name)
{
return Get-Project $name
}
$startupProjectPaths = $DTE.Solution.SolutionBuild.StartupProjects
if ($startupProjectPaths)
{
if ($startupProjectPaths.Length -eq 1)
{
$startupProjectPath = $startupProjectPaths[0]
if (![IO.Path]::IsPathRooted($startupProjectPath))
{
$solutionPath = Split-Path (GetProperty $DTE.Solution.Properties 'Path')
$startupProjectPath = Join-Path $solutionPath $startupProjectPath -Resolve | Convert-Path
}
$startupProject = GetSolutionProjects |
?{
try
{
$fullName = $_.FullName
}
catch [NotImplementedException]
{
return $false
}
if ($fullName -and $fullName.EndsWith('\'))
{
$fullName = $fullName.Substring(0, $fullName.Length - 1)
}
return $fullName -eq $startupProjectPath
}
if ($startupProject)
{
return $startupProject
}
Write-Warning "Unable to resolve startup project '$startupProjectPath'."
}
else
{
Write-Warning 'Multiple startup projects set.'
}
}
else
{
Write-Warning 'No startup project set.'
}
Write-Warning "Using project '$($fallbackProject.ProjectName)' as the startup project."
return $fallbackProject
}
function GetSolutionProjects()
{
$projects = New-Object 'System.Collections.Stack'
$DTE.Solution.Projects |
%{ $projects.Push($_) }
while ($projects.Count)
{
$project = $projects.Pop();
<# yield return #> $project
if ($project.ProjectItems)
{
$project.ProjectItems |
?{ $_.SubProject } |
%{ $projects.Push($_.SubProject) }
}
}
}
function GetParams($connectionStringName, $connectionString, $connectionProviderName)
{
$params = @()
if ($connectionStringName)
{
$params += '--connection-string-name', $connectionStringName
}
if ($connectionString)
{
$params += '--connection-string', $connectionString,
'--connection-provider', $connectionProviderName
}
return $params
}
function ShowConsole
{
$componentModel = Get-VSComponentModel
$powerConsoleWindow = $componentModel.GetService([NuGetConsole.IPowerConsoleWindow])
$powerConsoleWindow.Show()
}
function WriteErrorLine($message)
{
try
{
# Call the internal API NuGet uses to display errors
$componentModel = Get-VSComponentModel
$powerConsoleWindow = $componentModel.GetService([NuGetConsole.IPowerConsoleWindow])
$bindingFlags = [Reflection.BindingFlags]::Instance -bor [Reflection.BindingFlags]::NonPublic
$activeHostInfo = $powerConsoleWindow.GetType().GetProperty('ActiveHostInfo', $bindingFlags).GetValue($powerConsoleWindow)
$internalHost = $activeHostInfo.WpfConsole.Host
$reportErrorMethod = $internalHost.GetType().GetMethod('ReportError', $bindingFlags, $null, [Exception], $null)
$exception = New-Object Exception $message
$reportErrorMethod.Invoke($internalHost, $exception)
}
catch
{
Write-Host $message -ForegroundColor DarkRed
}
}
function EF6($project, $startupProject, $workingDir, $params)
{
$solutionBuild = $DTE.Solution.SolutionBuild
$solutionBuild.BuildProject(
$solutionBuild.ActiveConfiguration.Name,
$project.UniqueName,
<# WaitForBuildToFinish #> $true)
if ($solutionBuild.LastBuildInfo)
{
throw "The project '$($project.ProjectName)' failed to build."
}
$projectDir = GetProperty $project.Properties 'FullPath'
$outputPath = GetProperty $project.ConfigurationManager.ActiveConfiguration.Properties 'OutputPath'
$targetDir = [IO.Path]::GetFullPath([IO.Path]::Combine($projectDir, $outputPath))
$targetFrameworkMoniker = GetProperty $project.Properties 'TargetFrameworkMoniker'
$frameworkName = New-Object 'System.Runtime.Versioning.FrameworkName' $targetFrameworkMoniker
$targetFrameworkIdentifier = $frameworkName.Identifier
$targetFrameworkVersion = $frameworkName.Version
if ($targetFrameworkIdentifier -in '.NETFramework')
{
if ($targetFrameworkVersion -lt '4.5')
{
$frameworkDir = 'net40'
}
else
{
$frameworkDir = 'net45'
}
$platformTarget = GetPlatformTarget $project
if ($platformTarget -eq 'x86')
{
$runtimeDir = 'win-x86'
}
elseif ($platformTarget -in 'AnyCPU', 'x64')
{
$runtimeDir = 'any'
}
else
{
throw "Project '$($project.ProjectName)' has an active platform of '$platformTarget'. Select a different " +
'platform and try again.'
}
$exePath = Join-Path $PSScriptRoot "$frameworkDir\$runtimeDir\ef6.exe"
}
elseif ($targetFrameworkIdentifier -eq '.NETCoreApp')
{
$exePath = (Get-Command 'dotnet').Path
$targetName = GetProperty $project.Properties 'AssemblyName'
$depsFile = Join-Path $targetDir ($targetName + '.deps.json')
$projectAssetsFile = GetCpsProperty $project 'ProjectAssetsFile'
$runtimeConfig = Join-Path $targetDir ($targetName + '.runtimeconfig.json')
$runtimeFrameworkVersion = GetCpsProperty $project 'RuntimeFrameworkVersion'
$efPath = Join-Path $PSScriptRoot 'netcoreapp3.0\any\ef6.dll'
$dotnetParams = 'exec', '--depsfile', $depsFile
if ($projectAssetsFile)
{
# NB: Don't use Get-Content. It doesn't handle UTF-8 without a signature
# NB: Don't use ReadAllLines. ConvertFrom-Json won't work on PowerShell 3.0
$projectAssets = [IO.File]::ReadAllText($projectAssetsFile) | ConvertFrom-Json
$projectAssets.packageFolders.psobject.Properties.Name |
%{ $dotnetParams += '--additionalprobingpath', $_.TrimEnd('\') }
}
if (Test-Path $runtimeConfig)
{
$dotnetParams += '--runtimeconfig', $runtimeConfig
}
elseif ($runtimeFrameworkVersion)
{
$dotnetParams += '--fx-version', $runtimeFrameworkVersion
}
$dotnetParams += $efPath
$params = $dotnetParams + $params
}
else
{
throw "Project '$($startupProject.ProjectName)' targets framework '$targetFrameworkIdentifier'. The Entity Framework " +
'Package Manager Console Tools don''t support this framework.'
}
$targetFileName = GetProperty $project.Properties 'OutputFileName'
$targetPath = Join-Path $targetDir $targetFileName
$rootNamespace = GetProperty $project.Properties 'RootNamespace'
$language = GetLanguage $project
$params += '--verbose',
'--no-color',
'--prefix-output',
'--assembly', $targetPath,
'--project-dir', $projectDir,
'--language', $language
if (IsWeb $startupProject)
{
$startupProjectDir = GetProperty $startupProject.Properties 'FullPath'
$params += '--data-dir', (Join-Path $startupProjectDir 'App_Data')
}
if ($rootNamespace)
{
$params += '--root-namespace', $rootNamespace
}
$configFile = GetConfigPath $startupProject
if ($configFile)
{
$params += '--config', $configFile
}
if (!$workingDir)
{
$workingDir = $targetDir
}
$arguments = ToArguments $params
$startInfo = New-Object 'System.Diagnostics.ProcessStartInfo' -Property @{
FileName = $exePath;
Arguments = $arguments;
UseShellExecute = $false;
CreateNoWindow = $true;
RedirectStandardOutput = $true;
StandardOutputEncoding = [Text.Encoding]::UTF8;
RedirectStandardError = $true;
WorkingDirectory = $workingDir;
}
Write-Verbose "$exePath $arguments"
$process = [Diagnostics.Process]::Start($startInfo)
while (($line = $process.StandardOutput.ReadLine()) -ne $null)
{
$level = $null
$text = $null
$parts = $line.Split(':', 2)
if ($parts.Length -eq 2)
{
$level = $parts[0]
$i = 0
$count = 8 - $level.Length
while ($i -lt $count -and $parts[1][$i] -eq ' ')
{
$i++
}
$text = $parts[1].Substring($i)
}
switch ($level)
{
'error' { WriteErrorLine $text }
'warn' { Write-Warning $text }
'info' { Write-Host $text }
'data' { Write-Output $text }
'verbose' { Write-Verbose $text }
default { Write-Host $line }
}
}
$process.WaitForExit()
if ($process.ExitCode)
{
while (($line = $process.StandardError.ReadLine()) -ne $null)
{
WriteErrorLine $line
}
exit
}
}
function IsCpsProject($project)
{
$hierarchy = GetVsHierarchy $project
$isCapabilityMatch = [Microsoft.VisualStudio.Shell.PackageUtilities].GetMethod(
'IsCapabilityMatch',
[type[]]([Microsoft.VisualStudio.Shell.Interop.IVsHierarchy], [string]))
return $isCapabilityMatch.Invoke($null, ($hierarchy, 'CPS'))
}