forked from dotnet/ef6
-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EFTools: Fallback to config when looking for EF6 providers
Work Item: 2506
- Loading branch information
Showing
10 changed files
with
155 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
src/EntityFramework/Infrastructure/Design/AppConfigReader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
namespace System.Data.Entity.Infrastructure.Design | ||
{ | ||
using System.Configuration; | ||
using System.Data.Entity.Internal; | ||
using System.Data.Entity.Internal.ConfigFile; | ||
using System.Data.Entity.Utilities; | ||
using System.Linq; | ||
|
||
/// <summary> | ||
/// Provides utility methods for reading from an App.config or Web.config file. | ||
/// </summary> | ||
public class AppConfigReader | ||
{ | ||
private readonly Configuration _configuration; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of <see cref="AppConfigReader" />. | ||
/// </summary> | ||
/// <param name="configuration">The configuration to read from.</param> | ||
public AppConfigReader(Configuration configuration) | ||
{ | ||
Check.NotNull(configuration, "configuration"); | ||
|
||
_configuration = configuration; | ||
} | ||
|
||
/// <summary> | ||
/// Gets the specified provider services from the configuration. | ||
/// </summary> | ||
/// <param name="invariantName">The invariant name of the provider services.</param> | ||
/// <returns>The provider services type name, or null if not found.</returns> | ||
public string GetProviderServices(string invariantName) | ||
{ | ||
var providers = ((EntityFrameworkSection)_configuration.GetSection(AppConfig.EFSectionName)) | ||
.Providers.Cast<ProviderElement>(); | ||
|
||
return (from p in providers | ||
where p.InvariantName == invariantName | ||
select p.ProviderTypeName) | ||
.FirstOrDefault(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
test/EntityFramework/UnitTests/Infrastructure/Design/AppConfigReaderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information. | ||
|
||
namespace System.Data.Entity.Infrastructure.Design | ||
{ | ||
using System.Configuration; | ||
using System.IO; | ||
using Xunit; | ||
|
||
public class AppConfigReaderTests | ||
{ | ||
[Fact] | ||
public void Ctor_validates_parameter() | ||
{ | ||
var ex = Assert.Throws<ArgumentNullException>(() => new AppConfigReader(null)); | ||
|
||
Assert.Equal("configuration", ex.ParamName); | ||
} | ||
|
||
[Fact] | ||
public void GetProviderServices_returns_provider_when_exists() | ||
{ | ||
var reader = new AppConfigReader( | ||
CreateConfig("<provider invariantName='My.Invariant1' type='MyProvider1'/>")); | ||
|
||
var provider = reader.GetProviderServices("My.Invariant1"); | ||
|
||
Assert.Equal("MyProvider1", provider); | ||
} | ||
|
||
[Fact] | ||
public void GetProviderServices_returns_null_when_not_exists() | ||
{ | ||
var reader = new AppConfigReader( | ||
CreateConfig("<provider invariantName='My.Invariant1' type='MyProvider1'/>")); | ||
|
||
var provider = reader.GetProviderServices("My.Invariant2"); | ||
|
||
Assert.Null(provider); | ||
} | ||
|
||
private static Configuration CreateConfig(string providers) | ||
{ | ||
var file = Path.GetTempFileName(); | ||
File.WriteAllText( | ||
file, | ||
@"<?xml version='1.0' encoding='utf-8'?> | ||
<configuration> | ||
<configSections> | ||
<section name='entityFramework' type='System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework' /> | ||
</configSections> | ||
<entityFramework> | ||
<providers> | ||
" + providers + @" | ||
</providers> | ||
</entityFramework> | ||
</configuration>"); | ||
|
||
return ConfigurationManager.OpenMappedExeConfiguration( | ||
new ExeConfigurationFileMap { ExeConfigFilename = file }, | ||
ConfigurationUserLevel.None); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters