// 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); }
public void Read_DataSource_From_Closed_Connection() { var connectionString = JetConnection.GetConnectionString(StoreName, Helpers.DataAccessProviderFactory); var dataAccessProviderType = JetConnection.GetDataAccessProviderType(connectionString); var dataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType); var connectionStringBuilder = dataAccessProviderFactory.CreateConnectionStringBuilder(); connectionStringBuilder.ConnectionString = connectionString; var fileName = connectionStringBuilder.GetDataSource(); using var connection = new JetConnection(connectionString); Assert.AreEqual(fileName, connection.DataSource); }
public void Throws_when_no_initial_catalog() { var dataAccessProviderType = JetConnection.GetDataAccessProviderType(TestEnvironment.DefaultConnection); var dataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType); var connectionStringBuilder = dataAccessProviderFactory.CreateConnectionStringBuilder(); connectionStringBuilder.ConnectionString = TestEnvironment.DefaultConnection; connectionStringBuilder.Remove("Initial Catalog"); var creator = GetDatabaseCreator(connectionStringBuilder.ToString()); var ex = Assert.Throws <InvalidOperationException>(() => creator.Delete()); Assert.Equal(JetStrings.NoInitialCatalog, ex.Message); }
public NavigationTestFixture() { var serviceProvider = new ServiceCollection() .AddEntityFrameworkJet() .BuildServiceProvider(validateScopes: true); var dataAccessProviderType = JetConnection.GetDataAccessProviderType(TestEnvironment.DefaultConnection); var dataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(dataAccessProviderType); var connectionStringBuilder = dataAccessProviderFactory.CreateConnectionStringBuilder(); connectionStringBuilder.ConnectionString = TestEnvironment.DefaultConnection; connectionStringBuilder.SetDataSource("StateManagerBug.accdb"); _options = new DbContextOptionsBuilder() .UseJet(connectionStringBuilder.ConnectionString, TestEnvironment.DataAccessProviderFactory, b => b.ApplyConfiguration()) .UseInternalServiceProvider(serviceProvider) .Options; }
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); }
private JetTestStore( string name, string scriptPath = null, string templatePath = null, bool shared = true) : base(name + ".accdb", shared) { if (scriptPath != null) { _scriptPath = Path.Combine( Path.GetDirectoryName( typeof(JetTestStore).GetTypeInfo() .Assembly.Location), scriptPath); } if (templatePath != null) { _templatePath = Path.Combine( Path.GetDirectoryName( typeof(JetTestStore).GetTypeInfo() .Assembly.Location), templatePath); } ConnectionString = CreateConnectionString(Name); var dataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(JetConnection.GetDataAccessProviderType(ConnectionString)); var connection = (JetConnection)JetFactory.Instance.CreateConnection(); connection.ConnectionString = ConnectionString; connection.DataAccessProviderFactory = dataAccessProviderFactory; Connection = connection; }
public static string CreateConnectionString(string name) { var defaultConnectionString = TestEnvironment.DefaultConnection; var dataAccessProviderFactory = JetFactory.Instance.GetDataAccessProviderFactory(JetConnection.GetDataAccessProviderType(defaultConnectionString)); var connectionStringBuilder = dataAccessProviderFactory.CreateConnectionStringBuilder(); connectionStringBuilder.ConnectionString = defaultConnectionString; connectionStringBuilder.SetDataSource(name); return(connectionStringBuilder.ToString()); }