Fix an issue where we were always using the (changeable) static variable `JetConfiguration.OleDbDefaultProvider` for database create operations.

pull/37/head
Lau 6 years ago
parent 42c4aabdfd
commit 3c692bb172

@ -10,5 +10,6 @@ namespace EntityFrameworkCore.Jet.Infrastructure.Internal
/// </summary>
public interface IJetOptions : ISingletonOptions
{
string ConnectionString { get; }
}
}

@ -18,6 +18,8 @@ namespace EntityFrameworkCore.Jet.Internal
/// </summary>
public virtual void Initialize(IDbContextOptions options)
{
var jetOptions = options.FindExtension<JetOptionsExtension>() ?? new JetOptionsExtension();
ConnectionString = jetOptions.Connection?.ConnectionString ?? jetOptions.ConnectionString;
}
/// <summary>
@ -28,5 +30,27 @@ namespace EntityFrameworkCore.Jet.Internal
{
}
public virtual string ConnectionString { get; private set; }
protected bool Equals(JetOptions other)
{
return ConnectionString == other.ConnectionString;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
return false;
if (ReferenceEquals(this, obj))
return true;
if (obj.GetType() != this.GetType())
return false;
return Equals((JetOptions) obj);
}
public override int GetHashCode()
{
return (ConnectionString != null ? ConnectionString.GetHashCode() : 0);
}
}
}

@ -2,9 +2,12 @@
using System;
using System.Collections.Generic;
using System.Data.Jet;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using EntityFrameworkCore.Jet.Infrastructure.Internal;
using EntityFrameworkCore.Jet.Metadata;
using EntityFrameworkCore.Jet.Metadata.Internal;
using EntityFrameworkCore.Jet.Migrations.Operations;
@ -25,16 +28,19 @@ namespace EntityFrameworkCore.Jet.Migrations
public class JetMigrationsSqlGenerator : MigrationsSqlGenerator
{
private readonly IMigrationsAnnotationProvider _migrationsAnnotations;
[NotNull] private readonly IJetOptions _options;
private IReadOnlyList<MigrationOperation> _operations;
private int _variableCounter;
public JetMigrationsSqlGenerator(
[NotNull] MigrationsSqlGeneratorDependencies dependencies,
[NotNull] IMigrationsAnnotationProvider migrationsAnnotations)
[NotNull] IMigrationsAnnotationProvider migrationsAnnotations,
[NotNull] IJetOptions options)
: base(dependencies)
{
_migrationsAnnotations = migrationsAnnotations;
_options = options;
}
@ -448,11 +454,14 @@ namespace EntityFrameworkCore.Jet.Migrations
Check.NotNull(operation, nameof(operation));
Check.NotNull(builder, nameof(builder));
var connectionStringBuilder = new OleDbConnectionStringBuilder(_options.ConnectionString);
var provider = string.IsNullOrEmpty(connectionStringBuilder.Provider)
? JetConfiguration.OleDbDefaultProvider
: connectionStringBuilder.Provider;
builder
.Append("CREATE DATABASE ")
.Append(JetConnection.GetConnectionString(ExpandFileName(operation.Name)));
.Append(JetConnection.GetConnectionString(provider, ExpandFileName(operation.Name)));
builder
.AppendLine(Dependencies.SqlGenerationHelper.StatementTerminator)

@ -390,6 +390,10 @@ namespace System.Data.Jet
AdoxWrapper.CreateEmptyDatabase(connectionString);
}
public static string GetConnectionString(string provider, string fileName)
{
return $"Provider={provider};Data Source={fileName}";
}
public static string GetConnectionString(string fileName)
{

@ -13,31 +13,28 @@ namespace System.Data.Jet.JetStoreSchemaDefinition
static JetStoreDatabaseHandling()
{
_regExIsCreateOrDropDatabaseCommand = new Regex(
@"(^\s*create\s*database\s*.*$)+|(^drop\s*database\s*.*$)",
@"^\s*(?:create|drop)\s+database\s",
RegexOptions.IgnoreCase);
_regExParseCreateDatabaseCommand = new Regex(
@"^\s*create\s*database\s*(?<filename>.*)\s*$",
@"^\s*create\s+database\s+(?<filename>.*?)\s*;*\s*$",
RegexOptions.IgnoreCase);
_regExParseDropDatabaseCommand = new Regex(
@"^\s*drop\s*database\s*(?<filename>.*)\s*;*\s*$",
@"^\s*drop\s+database\s+(?<filename>.*?)\s*;*\s*$",
RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
_regExParseCreateDatabaseCommandFromConnection = new Regex(
@"^\s*create\s*database\s*(?<connectionString>provider\s*=\s*.*)\s*$",
@"^\s*create\s+database\s+(?<connectionString>provider\s*=\s*.*?)\s*$",
RegexOptions.IgnoreCase);
_regExParseDropDatabaseCommandFromConnection = new Regex(
@"^\s*drop\s*database\s*(?<connectionString>provider\s*=\s*.*)\s*$",
@"^\s*drop\s+database\s+(?<connectionString>provider\s*=\s*.*?)\s*$",
RegexOptions.IgnoreCase);
_regExExtractFilenameFromConnectionString = new Regex(
@"provider=.*;\s*data\s+source\s*=\s*(?<filename>[^;]*)\s*;?.*$",
@"provider\s*=\s*.*?;\s*data\s+source\s*=\s*(?<filename>.*?)\s*(?=;|$)",
RegexOptions.IgnoreCase);
}
public static bool TryDatabaseOperation(string commandText)

Loading…
Cancel
Save