3
3
4
4
using System . Collections ;
5
5
using System . Reflection ;
6
+ #if ! NET472
7
+ using System . Runtime . Loader ;
8
+ #endif
6
9
using Microsoft . EntityFrameworkCore . Design ;
7
10
using Microsoft . EntityFrameworkCore . Design . Internal ;
8
11
using Microsoft . EntityFrameworkCore . Infrastructure ;
@@ -17,10 +20,15 @@ internal class ReflectionOperationExecutor : OperationExecutorBase
17
20
private const string ReportHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationReportHandler" ;
18
21
private const string ResultHandlerTypeName = "Microsoft.EntityFrameworkCore.Design.OperationResultHandler" ;
19
22
private readonly Type _resultHandlerType ;
23
+ private string ? _efcoreVersion ;
24
+ #if ! NET472
25
+ private AssemblyLoadContext ? _assemblyLoadContext ;
26
+ #endif
20
27
21
28
public ReflectionOperationExecutor (
22
29
string assembly ,
23
30
string ? startupAssembly ,
31
+ string ? designAssembly ,
24
32
string ? project ,
25
33
string ? projectDir ,
26
34
string ? dataDirectory ,
@@ -29,7 +37,7 @@ public ReflectionOperationExecutor(
29
37
bool nullable ,
30
38
string [ ] remainingArguments ,
31
39
IOperationReportHandler reportHandler )
32
- : base ( assembly , startupAssembly , project , projectDir , rootNamespace , language , nullable , remainingArguments , reportHandler )
40
+ : base ( assembly , startupAssembly , designAssembly , project , projectDir , rootNamespace , language , nullable , remainingArguments , reportHandler )
33
41
{
34
42
var reporter = new OperationReporter ( reportHandler ) ;
35
43
var configurationFile = ( startupAssembly ?? assembly ) + ".config" ;
@@ -76,6 +84,62 @@ public ReflectionOperationExecutor(
76
84
_resultHandlerType = _commandsAssembly . GetType ( ResultHandlerTypeName , throwOnError : true , ignoreCase : false ) ! ;
77
85
}
78
86
87
+ #if ! NET472
88
+ protected AssemblyLoadContext AssemblyLoadContext
89
+ {
90
+ get
91
+ {
92
+ if ( _assemblyLoadContext != null )
93
+ {
94
+ return _assemblyLoadContext ;
95
+ }
96
+
97
+ if ( DesignAssemblyPath != null )
98
+ {
99
+ AssemblyLoadContext . Default . Resolving += ( context , name ) =>
100
+ {
101
+ var assemblyPath = Path . GetDirectoryName ( DesignAssemblyPath ) ! ;
102
+ assemblyPath = Path . Combine ( assemblyPath , name . Name + ".dll" ) ;
103
+ return File . Exists ( assemblyPath ) ? context . LoadFromAssemblyPath ( assemblyPath ) : null ;
104
+ } ;
105
+ _assemblyLoadContext = AssemblyLoadContext . Default ;
106
+ }
107
+
108
+ return AssemblyLoadContext . Default ;
109
+ }
110
+ }
111
+ #endif
112
+
113
+ public override string ? EFCoreVersion
114
+ {
115
+ get
116
+ {
117
+ if ( _efcoreVersion != null )
118
+ {
119
+ return _efcoreVersion ;
120
+ }
121
+
122
+ Assembly ? assembly = null ;
123
+ #if ! NET472
124
+ assembly = AssemblyLoadContext . LoadFromAssemblyName ( new AssemblyName ( DesignAssemblyName ) ) ;
125
+ #else
126
+ if ( DesignAssemblyPath != null )
127
+ {
128
+ var assemblyPath = Path . GetDirectoryName ( DesignAssemblyPath ) ;
129
+ assemblyPath = Path . Combine ( assemblyPath , DesignAssemblyName + ".dll" ) ;
130
+ assembly = Assembly . LoadFrom ( assemblyPath ) ;
131
+ }
132
+ else
133
+ {
134
+ assembly = Assembly . Load ( DesignAssemblyName ) ;
135
+ }
136
+ #endif
137
+ _efcoreVersion = assembly . GetCustomAttribute < AssemblyInformationalVersionAttribute > ( )
138
+ ? . InformationalVersion ;
139
+ return _efcoreVersion ;
140
+ }
141
+ }
142
+
79
143
protected override object CreateResultHandler ( )
80
144
=> Activator . CreateInstance ( _resultHandlerType ) ! ;
81
145
0 commit comments