Skip to content

Commit

Permalink
Key search in rawOptions done case insensitively
Browse files Browse the repository at this point in the history
  • Loading branch information
smitpatel committed Dec 10, 2014
1 parent 3c7d705 commit c1e3914
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/EntityFramework.Core/DbContextOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DbContextOptions : IDbContextOptions
public DbContextOptions()
{
_extensions = new List<DbContextOptionsExtension>();
_rawOptions = ImmutableDictionary<string, string>.Empty;
_rawOptions = ImmutableDictionary<string, string>.Empty.WithComparers(StringComparer.OrdinalIgnoreCase);
}

protected DbContextOptions([NotNull] DbContextOptions copyFrom)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public virtual IReadOnlyDictionary<string, string> ReadRawOptions(
Check.NotNull(configuration, "configuration");
Check.NotNull(contextType, "contextType");

var options = currentOptions.ToDictionary(i => i.Key, i => i.Value);
var options = currentOptions.ToDictionary(i => i.Key, i => i.Value, StringComparer.OrdinalIgnoreCase);

ReadRawOptions(options, configuration, string.Concat(
EntityFrameworkKey, Constants.KeyDelimiter, contextType.Name));
Expand Down
38 changes: 28 additions & 10 deletions test/EntityFramework.Core.Tests/DbContextOptionsParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Data.Entity.Infrastructure;
using Microsoft.Framework.ConfigurationModel;
using Xunit;
using System;

namespace Microsoft.Data.Entity.Tests
{
Expand All @@ -21,7 +22,7 @@ public void Connection_string_is_found_using_context_name()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -38,7 +39,7 @@ public void Connection_string_is_found_using_context_full_name()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -55,7 +56,7 @@ public void Connection_string_is_found_using_context_name_generic()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -72,7 +73,7 @@ public void Connection_string_is_found_using_context_full_name_generic()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -94,7 +95,7 @@ public void Indirect_connection_string_is_found_using_context_name()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -112,7 +113,7 @@ public void Indirect_connection_string_is_found_using_context_full_name()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -130,7 +131,7 @@ public void Indirect_connection_string_is_found_using_context_name_generic()
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -148,7 +149,7 @@ public void Indirect_connection_string_is_found_using_context_full_name_generic(
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>());
var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Expand All @@ -165,7 +166,7 @@ public void Existing_options_are_updated()
}
};

var currentOptions = new Dictionary<string, string> { { "Foo", "Goo" } };
var currentOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "Foo", "Goo" } };

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), currentOptions);

Expand All @@ -185,13 +186,30 @@ public void Existing_options_are_updated_generic()
}
};

var currentOptions = new Dictionary<string, string> { { "Foo", "Goo" } };
var currentOptions = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase) { { "Foo", "Goo" } };

var rawOptions = new DbContextOptionsParser().ReadRawOptions<MyContext>(config, currentOptions);

Assert.Equal(2, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
Assert.Equal("Goo", rawOptions["Foo"]);
}

[Fact]
public void Key_searching_is_case_insensitive() {
var config = new Configuration
{
new MemoryConfigurationSource
{
{ "entityFramework:" + typeof(MyContext).Name + ":connectionString", "MyConnectionString" }
}
};

var rawOptions = new DbContextOptionsParser().ReadRawOptions(config, typeof(MyContext), new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase));

Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
}

}
}
29 changes: 29 additions & 0 deletions test/EntityFramework.Core.Tests/DbContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1341,6 +1341,35 @@ public void Context_activation_reads_options_from_configuration_with_key_redirec
}
}

[Fact]
public void Context_activation_reads_options_from_configuration_case_insensitively()
{
var configSource = new MemoryConfigurationSource();
configSource.Add("entityFramework:contextWithDefaults:connectionString", "MyConnectionString");

var config = new Configuration();
config.Add(configSource);

var services = new ServiceCollection();

services
.AddEntityFramework(config)
.AddDbContext<ContextWithDefaults>();

var serviceProvider = services.BuildServiceProvider();

using (var context = serviceProvider.GetRequiredService<ContextWithDefaults>())
{
var contextServices = ((IDbContextServices)context).ScopedServiceProvider;
var contextOptions = (DbContextOptions<ContextWithDefaults>)contextServices.GetRequiredService<DbContextService<IDbContextOptions>>().Service;

Assert.NotNull(contextOptions);
var rawOptions = ((IDbContextOptions)contextOptions).RawOptions;
Assert.Equal(1, rawOptions.Count);
Assert.Equal("MyConnectionString", rawOptions["ConnectionString"]);
}
}

private class FakeService
{
}
Expand Down

0 comments on commit c1e3914

Please sign in to comment.