// Note: Decision made to use DbConnection not SqlConnection: Issue #772 /// <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="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 UseJet( [NotNull] this DbContextOptionsBuilder optionsBuilder, [NotNull] DbConnection connection, [CanBeNull] Action <JetDbContextOptionsBuilder> jetOptionsAction = null) { Check.NotNull(optionsBuilder, nameof(optionsBuilder)); Check.NotNull(connection, nameof(connection)); var jetConnection = connection as JetConnection; if (jetConnection == null) { throw new ArgumentException($"The {nameof(connection)} parameter must be of type {nameof(JetConnection)}."); } if (jetConnection.DataAccessProviderFactory == null) { var fileNameOrConnectionString = jetConnection.ConnectionString; DataAccessProviderType dataAccessProviderType; if (JetConnection.IsConnectionString(fileNameOrConnectionString)) { dataAccessProviderType = JetConnection.GetDataAccessProviderType(fileNameOrConnectionString); } else if (JetConnection.IsFileName(fileNameOrConnectionString)) { dataAccessProviderType = JetConfiguration.DefaultDataAccessProviderType; } else { 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."); } 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); jetOptionsAction?.Invoke(new JetDbContextOptionsBuilder(optionsBuilder)); return(optionsBuilder); }
private static DbContextOptionsBuilder UseJetCore( [NotNull] DbContextOptionsBuilder optionsBuilder, [NotNull] string fileNameOrConnectionString, [CanBeNull] DbProviderFactory dataAccessProviderFactory, [CanBeNull] DataAccessProviderType?dataAccessProviderType, Action <JetDbContextOptionsBuilder> jetOptionsAction) { Check.NotNull(optionsBuilder, nameof(optionsBuilder)); Check.NotEmpty(fileNameOrConnectionString, nameof(fileNameOrConnectionString)); if (dataAccessProviderFactory == null && dataAccessProviderType == null) { if (JetConnection.IsConnectionString(fileNameOrConnectionString)) { dataAccessProviderType = JetConnection.GetDataAccessProviderType(fileNameOrConnectionString); } else if (JetConnection.IsFileName(fileNameOrConnectionString)) { dataAccessProviderType = JetConfiguration.DefaultDataAccessProviderType; } else { throw new ArgumentException($"Either {nameof(dataAccessProviderFactory)} or {nameof(dataAccessProviderType)} must not be null, or a file name must be specified for {nameof(fileNameOrConnectionString)}."); } } var extension = (JetOptionsExtension)GetOrCreateExtension(optionsBuilder) .WithConnectionString(fileNameOrConnectionString); extension = extension.WithDataAccessProviderFactory( dataAccessProviderFactory ?? JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType.Value)); ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension); ConfigureWarnings(optionsBuilder); jetOptionsAction?.Invoke(new JetDbContextOptionsBuilder(optionsBuilder)); return(optionsBuilder); }