Update existing and add new UseJet signatures.

pull/46/head
Lau 6 years ago
parent 3f19b269b4
commit f86cf6e0b4

@ -18,12 +18,71 @@ namespace Microsoft.EntityFrameworkCore
/// </summary>
public static class JetDbContextOptionsBuilderExtensions
{
#region Connection String
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. The underlying data
/// access provider (ODBC or OLE DB) will be inferred from the style of this connection string. </param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>) UseJet((DbContextOptionsBuilder) optionsBuilder, connectionString, jetOptionsAction);
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. The underlying data
/// access provider (ODBC or OLE DB) will be inferred from the style of this connection string. </param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder UseJet(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));
return UseJetCore(optionsBuilder, connectionString, null, JetConnection.GetDataAccessProviderType(connectionString), jetOptionsAction);
}
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderFactory">A `OdbcFactory` or `OleDbFactory` object to be used for all
/// <param name="dataAccessProviderFactory">An `OdbcFactory` or `OleDbFactory` object to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string connectionString,
[NotNull] DbProviderFactory dataAccessProviderFactory,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(dataAccessProviderFactory, nameof(dataAccessProviderFactory));
return (DbContextOptionsBuilder<TContext>) UseJet((DbContextOptionsBuilder) optionsBuilder, connectionString, dataAccessProviderFactory, jetOptionsAction);
}
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderFactory">An `OdbcFactory` or `OleDbFactory` object to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
@ -37,22 +96,78 @@ namespace Microsoft.EntityFrameworkCore
Check.NotEmpty(connectionString, nameof(connectionString));
Check.NotNull(dataAccessProviderFactory, nameof(dataAccessProviderFactory));
return UseJetCore(optionsBuilder, connectionString, dataAccessProviderFactory, jetOptionsAction);
return UseJetCore(optionsBuilder, connectionString, dataAccessProviderFactory, null, jetOptionsAction);
}
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderType">The type of the data access provider (`Odbc` or `OleDb`) to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string connectionString,
DataAccessProviderType dataAccessProviderType,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));
return (DbContextOptionsBuilder<TContext>) UseJet((DbContextOptionsBuilder)optionsBuilder, connectionString, dataAccessProviderType, jetOptionsAction);
}
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderType">The type of the data access provider (`Odbc` or `OleDb`) to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder UseJet(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
DataAccessProviderType dataAccessProviderType,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));
return UseJetCore(optionsBuilder, connectionString, null, dataAccessProviderType, jetOptionsAction);
}
internal static DbContextOptionsBuilder UseJetWithoutPredefinedDataAccessProvider(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
=> UseJetCore(optionsBuilder, connectionString, null, null, jetOptionsAction);
private static DbContextOptionsBuilder UseJetCore(
[NotNull] DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] DbProviderFactory dataAccessProviderFactory,
[CanBeNull] DataAccessProviderType? dataAccessProviderType,
Action<JetDbContextOptionsBuilder> jetOptionsAction)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotEmpty(connectionString, nameof(connectionString));
if (dataAccessProviderFactory == null && dataAccessProviderType == null)
{
throw new ArgumentException($"One of the parameters {nameof(dataAccessProviderFactory)} and {nameof(dataAccessProviderType)} must not be null.");
}
var extension = (JetOptionsExtension) GetOrCreateExtension(optionsBuilder)
.WithConnectionString(connectionString);
extension = extension.WithDataAccessProviderFactory(dataAccessProviderFactory);
extension = extension.WithDataAccessProviderFactory(
dataAccessProviderFactory ?? JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType.Value));
((IDbContextOptionsBuilderInfrastructure) optionsBuilder).AddOrUpdateExtension(extension);
@ -63,6 +178,31 @@ namespace Microsoft.EntityFrameworkCore
return optionsBuilder;
}
#endregion
#region Connection
// Note: Decision made to use DbConnection not SqlConnection: Issue #772
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <typeparam name="TContext"> The type of context to be configured. </typeparam>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connection">
/// An existing <see cref="DbConnection" /> to be used to connect to the database. If the connection is
/// in the open state then EF will not open or close the connection. If the connection is in the closed
/// state then EF will open and close the connection as needed.
/// </param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] DbConnection connection,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>) UseJet(
(DbContextOptionsBuilder) optionsBuilder, connection, jetOptionsAction);
// Note: Decision made to use DbConnection not SqlConnection: Issue #772
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
@ -91,14 +231,16 @@ namespace Microsoft.EntityFrameworkCore
if (jetConnection.DataAccessProviderFactory == null)
{
throw new ArgumentException($"The {nameof(connection)} parameter of type {nameof(JetConnection)} must have its {nameof(JetConnection.DataAccessProviderFactory)} property set to OdbcFactory or OleDbFactory.");
var dataAccessProviderType = JetConnection.GetDataAccessProviderType(jetConnection.ConnectionString);
jetConnection.DataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType);
jetConnection.Freeze();
}
var extension = (JetOptionsExtension) GetOrCreateExtension(optionsBuilder)
.WithConnection(connection);
extension = extension.WithDataAccessProviderFactory(jetConnection.DataAccessProviderFactory);
((IDbContextOptionsBuilderInfrastructure) optionsBuilder).AddOrUpdateExtension(extension);
ConfigureWarnings(optionsBuilder);
@ -108,63 +250,8 @@ namespace Microsoft.EntityFrameworkCore
return optionsBuilder;
}
#endregion
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderFactory">A `OdbcFactory` or `OleDbFactory` object to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
internal static DbContextOptionsBuilder UseJetWithoutPredefinedDataAccessProviderFactory<TContext>(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string connectionString,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
=> UseJetCore(optionsBuilder, connectionString, null, jetOptionsAction);
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <typeparam name="TContext"> The type of context to be configured. </typeparam>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connectionString"> The connection string of the database to connect to. </param>
/// <param name="dataAccessProviderFactory">A `OdbcFactory` or `OleDbFactory` object to be used for all
/// data access operations by the Jet connection.</param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string connectionString,
[NotNull] DbProviderFactory dataAccessProviderFactory,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>) UseJet(
(DbContextOptionsBuilder) optionsBuilder, connectionString, dataAccessProviderFactory, jetOptionsAction);
// Note: Decision made to use DbConnection not SqlConnection: Issue #772
/// <summary>
/// Configures the context to connect to a Microsoft Jet database.
/// </summary>
/// <typeparam name="TContext"> The type of context to be configured. </typeparam>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="connection">
/// An existing <see cref="DbConnection" /> to be used to connect to the database. If the connection is
/// in the open state then EF will not open or close the connection. If the connection is in the closed
/// state then EF will open and close the connection as needed.
/// </param>
/// <param name="jetOptionsAction">An optional action to allow additional Jet specific configuration.</param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseJet<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] DbConnection connection,
[CanBeNull] Action<JetDbContextOptionsBuilder> jetOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>) UseJet(
(DbContextOptionsBuilder) optionsBuilder, connection, jetOptionsAction);
private static JetOptionsExtension GetOrCreateExtension(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.Options.FindExtension<JetOptionsExtension>()
?? new JetOptionsExtension();

@ -56,7 +56,7 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkJet()
.AddDbContext<DbContext>((p, o) => o
.UseJetWithoutPredefinedDataAccessProviderFactory<DbContext>(
.UseJetWithoutPredefinedDataAccessProvider(
JetConnection.GetConnectionString("Jet.accdb", DataAccessProviderType.Odbc))
.UseInternalServiceProvider(p))
.BuildServiceProvider();

@ -10,6 +10,7 @@ namespace System.Data.Jet
{
private ConnectionState _state;
private string _connectionString;
private bool _frozen;
internal DbConnection InnerConnection { get; private set; }
@ -175,6 +176,10 @@ namespace System.Data.Jet
{
if (State != ConnectionState.Closed)
throw new InvalidOperationException(Messages.CannotChangePropertyValueInThisConnectionState(nameof(ConnectionString), State));
if (_frozen)
throw new InvalidOperationException($"Cannot modify \"{nameof(ConnectionString)}\" property after the connection has been frozen.");
_connectionString = value;
}
}
@ -298,11 +303,15 @@ namespace System.Data.Jet
return;
if (string.IsNullOrWhiteSpace(_connectionString))
throw new InvalidOperationException(Messages.PropertyNotInitialized(nameof(ConnectionString)));
if (JetFactory == null)
throw new InvalidOperationException(Messages.PropertyNotInitialized(nameof(DataAccessProviderFactory)));
if (State != ConnectionState.Closed)
throw new InvalidOperationException(Messages.CannotCallMethodInThisConnectionState(nameof(Open), ConnectionState.Closed, State));
if (JetFactory == null)
{
var dataAccessProviderType = GetDataAccessProviderType(ConnectionString);
DataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType);
}
try
{
InnerConnection = InnerConnectionFactory.Instance.OpenConnection(
@ -404,6 +413,12 @@ namespace System.Data.Jet
return clone;
}
public void Freeze()
{
if (!string.IsNullOrWhiteSpace(ConnectionString))
_frozen = true;
}
/// <summary>
/// Clears the pool.
/// </summary>

Loading…
Cancel
Save