@@ -24,147 +24,166 @@ public static class JetDbContextOptionsBuilderExtensions
24
24
/// Configures the context to connect to a Microsoft Jet database.
25
25
/// </summary>
26
26
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
27
- /// <param name="connectionString"> The connection string of the database to connect to. The underlying data
28
- /// access provider (ODBC or OLE DB) will be inferred from the style of this connection string. </param>
27
+ /// <param name="fileNameOrConnectionString"> The file name or connection string of the database to connect to.
28
+ /// If just a file name is supplied, the default data access provider type as defined by
29
+ /// `JetConfiguration.DefaultDataAccessProviderType` is being used. If a connection string is supplied, the
30
+ /// underlying data access provider (ODBC or OLE DB) will be inferred from the style of the connection string.
31
+ /// In case the connection string does not specify an Access driver (ODBC) or ACE/Jet provider (OLE DB), the
32
+ /// highest version of all compatible installed ones is being used. </param>
29
33
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
30
34
/// <returns> The options builder so that further configuration can be chained. </returns>
31
35
public static DbContextOptionsBuilder < TContext > UseJet < TContext > (
32
36
[ NotNull ] this DbContextOptionsBuilder < TContext > optionsBuilder ,
33
- [ NotNull ] string connectionString ,
37
+ [ NotNull ] string fileNameOrConnectionString ,
34
38
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
35
39
where TContext : DbContext
36
- => ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , connectionString , jetOptionsAction ) ;
40
+ => ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , fileNameOrConnectionString , jetOptionsAction ) ;
37
41
38
42
/// <summary>
39
43
/// Configures the context to connect to a Microsoft Jet database.
40
44
/// </summary>
41
45
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
42
- /// <param name="connectionString"> The connection string of the database to connect to. The underlying data
43
- /// access provider (ODBC or OLE DB) will be inferred from the style of this connection string. </param>
46
+ /// <param name="fileNameOrConnectionString"> The file name or connection string of the database to connect to.
47
+ /// If just a file name is supplied, the default data access provider type as defined by
48
+ /// `JetConfiguration.DefaultDataAccessProviderType` is being used. If a connection string is supplied, the
49
+ /// underlying data access provider (ODBC or OLE DB) will be inferred from the style of the connection string.
50
+ /// In case the connection string does not specify an Access driver (ODBC) or ACE/Jet provider (OLE DB), the
51
+ /// highest version of all compatible installed ones is being used. </param>
44
52
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
45
53
/// <returns> The options builder so that further configuration can be chained. </returns>
46
54
public static DbContextOptionsBuilder UseJet (
47
55
[ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
48
- [ NotNull ] string connectionString ,
56
+ [ NotNull ] string fileNameOrConnectionString ,
49
57
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
50
58
{
51
59
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
52
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
60
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
53
61
54
- return UseJetCore ( optionsBuilder , connectionString , null , JetConnection . GetDataAccessProviderType ( connectionString ) , jetOptionsAction ) ;
62
+ return UseJetCore ( optionsBuilder , fileNameOrConnectionString , null , null , jetOptionsAction ) ;
55
63
}
56
64
57
65
/// <summary>
58
66
/// Configures the context to connect to a Microsoft Jet database.
59
67
/// </summary>
60
68
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
61
- /// <param name="connectionString "> The connection string of the database to connect to. </param>
69
+ /// <param name="fileNameOrConnectionString "> The file name or connection string of the database to connect to.
62
70
/// <param name="dataAccessProviderFactory">An `OdbcFactory` or `OleDbFactory` object to be used for all
63
71
/// data access operations by the Jet connection.</param>
64
72
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
65
73
/// <returns> The options builder so that further configuration can be chained. </returns>
66
74
public static DbContextOptionsBuilder < TContext > UseJet < TContext > (
67
75
[ NotNull ] this DbContextOptionsBuilder < TContext > optionsBuilder ,
68
- [ NotNull ] string connectionString ,
76
+ [ NotNull ] string fileNameOrConnectionString ,
69
77
[ NotNull ] DbProviderFactory dataAccessProviderFactory ,
70
78
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
71
79
where TContext : DbContext
72
80
{
73
81
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
74
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
82
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
75
83
Check . NotNull ( dataAccessProviderFactory , nameof ( dataAccessProviderFactory ) ) ;
76
84
77
- return ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , connectionString , dataAccessProviderFactory , jetOptionsAction ) ;
85
+ return ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , fileNameOrConnectionString , dataAccessProviderFactory , jetOptionsAction ) ;
78
86
}
79
87
80
88
/// <summary>
81
89
/// Configures the context to connect to a Microsoft Jet database.
82
90
/// </summary>
83
91
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
84
- /// <param name="connectionString "> The connection string of the database to connect to. </param>
92
+ /// <param name="fileNameOrConnectionString "> The file name or connection string of the database to connect to.
85
93
/// <param name="dataAccessProviderFactory">An `OdbcFactory` or `OleDbFactory` object to be used for all
86
94
/// data access operations by the Jet connection.</param>
87
95
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
88
96
/// <returns> The options builder so that further configuration can be chained. </returns>
89
97
public static DbContextOptionsBuilder UseJet (
90
98
[ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
91
- [ NotNull ] string connectionString ,
99
+ [ NotNull ] string fileNameOrConnectionString ,
92
100
[ NotNull ] DbProviderFactory dataAccessProviderFactory ,
93
101
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
94
102
{
95
103
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
96
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
104
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
97
105
Check . NotNull ( dataAccessProviderFactory , nameof ( dataAccessProviderFactory ) ) ;
98
106
99
- return UseJetCore ( optionsBuilder , connectionString , dataAccessProviderFactory , null , jetOptionsAction ) ;
107
+ return UseJetCore ( optionsBuilder , fileNameOrConnectionString , dataAccessProviderFactory , null , jetOptionsAction ) ;
100
108
}
101
109
102
110
/// <summary>
103
111
/// Configures the context to connect to a Microsoft Jet database.
104
112
/// </summary>
105
113
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
106
- /// <param name="connectionString "> The connection string of the database to connect to. </param>
114
+ /// <param name="fileNameOrConnectionString "> The file name or connection string of the database to connect to.
107
115
/// <param name="dataAccessProviderType">The type of the data access provider (`Odbc` or `OleDb`) to be used for all
108
116
/// data access operations by the Jet connection.</param>
109
117
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
110
118
/// <returns> The options builder so that further configuration can be chained. </returns>
111
119
public static DbContextOptionsBuilder < TContext > UseJet < TContext > (
112
120
[ NotNull ] this DbContextOptionsBuilder < TContext > optionsBuilder ,
113
- [ NotNull ] string connectionString ,
121
+ [ NotNull ] string fileNameOrConnectionString ,
114
122
DataAccessProviderType dataAccessProviderType ,
115
123
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
116
124
where TContext : DbContext
117
125
{
118
126
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
119
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
127
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
120
128
121
- return ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , connectionString , dataAccessProviderType , jetOptionsAction ) ;
129
+ return ( DbContextOptionsBuilder < TContext > ) UseJet ( ( DbContextOptionsBuilder ) optionsBuilder , fileNameOrConnectionString , dataAccessProviderType , jetOptionsAction ) ;
122
130
}
123
131
124
132
/// <summary>
125
133
/// Configures the context to connect to a Microsoft Jet database.
126
134
/// </summary>
127
135
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
128
- /// <param name="connectionString "> The connection string of the database to connect to. </param>
136
+ /// <param name="fileNameOrConnectionString "> The file name or connection string of the database to connect to.
129
137
/// <param name="dataAccessProviderType">The type of the data access provider (`Odbc` or `OleDb`) to be used for all
130
138
/// data access operations by the Jet connection.</param>
131
139
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
132
140
/// <returns> The options builder so that further configuration can be chained. </returns>
133
141
public static DbContextOptionsBuilder UseJet (
134
142
[ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
135
- [ NotNull ] string connectionString ,
143
+ [ NotNull ] string fileNameOrConnectionString ,
136
144
DataAccessProviderType dataAccessProviderType ,
137
145
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
138
146
{
139
147
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
140
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
148
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
141
149
142
- return UseJetCore ( optionsBuilder , connectionString , null , dataAccessProviderType , jetOptionsAction ) ;
150
+ return UseJetCore ( optionsBuilder , fileNameOrConnectionString , null , dataAccessProviderType , jetOptionsAction ) ;
143
151
}
144
152
145
153
internal static DbContextOptionsBuilder UseJetWithoutPredefinedDataAccessProvider (
146
154
[ NotNull ] this DbContextOptionsBuilder optionsBuilder ,
147
- [ NotNull ] string connectionString ,
155
+ [ NotNull ] string fileNameOrConnectionString ,
148
156
[ CanBeNull ] Action < JetDbContextOptionsBuilder > jetOptionsAction = null )
149
- => UseJetCore ( optionsBuilder , connectionString , null , null , jetOptionsAction ) ;
157
+ => UseJetCore ( optionsBuilder , fileNameOrConnectionString , null , null , jetOptionsAction ) ;
150
158
151
159
private static DbContextOptionsBuilder UseJetCore (
152
160
[ NotNull ] DbContextOptionsBuilder optionsBuilder ,
153
- [ NotNull ] string connectionString ,
161
+ [ NotNull ] string fileNameOrConnectionString ,
154
162
[ CanBeNull ] DbProviderFactory dataAccessProviderFactory ,
155
163
[ CanBeNull ] DataAccessProviderType ? dataAccessProviderType ,
156
164
Action < JetDbContextOptionsBuilder > jetOptionsAction )
157
165
{
158
166
Check . NotNull ( optionsBuilder , nameof ( optionsBuilder ) ) ;
159
- Check . NotEmpty ( connectionString , nameof ( connectionString ) ) ;
167
+ Check . NotEmpty ( fileNameOrConnectionString , nameof ( fileNameOrConnectionString ) ) ;
160
168
161
169
if ( dataAccessProviderFactory == null && dataAccessProviderType == null )
162
170
{
163
- throw new ArgumentException ( $ "One of the parameters { nameof ( dataAccessProviderFactory ) } and { nameof ( dataAccessProviderType ) } must not be null.") ;
171
+ if ( JetConnection . IsConnectionString ( fileNameOrConnectionString ) )
172
+ {
173
+ dataAccessProviderType = JetConnection . GetDataAccessProviderType ( fileNameOrConnectionString ) ;
174
+ }
175
+ else if ( JetConnection . IsFileName ( fileNameOrConnectionString ) )
176
+ {
177
+ dataAccessProviderType = JetConfiguration . DefaultDataAccessProviderType ;
178
+ }
179
+ else
180
+ {
181
+ throw new ArgumentException ( $ "Either { nameof ( dataAccessProviderFactory ) } or { nameof ( dataAccessProviderType ) } must not be null, or a file name must be specified for { nameof ( fileNameOrConnectionString ) } .") ;
182
+ }
164
183
}
165
184
166
185
var extension = ( JetOptionsExtension ) GetOrCreateExtension ( optionsBuilder )
167
- . WithConnectionString ( connectionString ) ;
186
+ . WithConnectionString ( fileNameOrConnectionString ) ;
168
187
169
188
extension = extension . WithDataAccessProviderFactory (
170
189
dataAccessProviderFactory ?? JetFactory . Instance . GetDataAccessProviderFactory ( dataAccessProviderType . Value ) ) ;
@@ -231,7 +250,22 @@ public static DbContextOptionsBuilder UseJet(
231
250
232
251
if ( jetConnection . DataAccessProviderFactory == null )
233
252
{
234
- var dataAccessProviderType = JetConnection . GetDataAccessProviderType ( jetConnection . ConnectionString ) ;
253
+ var fileNameOrConnectionString = jetConnection . ConnectionString ;
254
+ DataAccessProviderType dataAccessProviderType ;
255
+
256
+ if ( JetConnection . IsConnectionString ( fileNameOrConnectionString ) )
257
+ {
258
+ dataAccessProviderType = JetConnection . GetDataAccessProviderType ( fileNameOrConnectionString ) ;
259
+ }
260
+ else if ( JetConnection . IsFileName ( fileNameOrConnectionString ) )
261
+ {
262
+ dataAccessProviderType = JetConfiguration . DefaultDataAccessProviderType ;
263
+ }
264
+ else
265
+ {
266
+ throw new ArgumentException ( $ "The data access provider type could not be inferred from the connections { nameof ( JetConnection . DataAccessProviderFactory ) } or { nameof ( JetConnection . ConnectionString ) } property and the { nameof ( JetConnection . ConnectionString ) } property is not a valid file name either.") ;
267
+ }
268
+
235
269
jetConnection . DataAccessProviderFactory = JetFactory . Instance . GetDataAccessProviderFactory ( dataAccessProviderType ) ;
236
270
jetConnection . Freeze ( ) ;
237
271
}
0 commit comments