Skip to content

Commit

Permalink
EFTools: Fallback to config when looking for EF6 providers
Browse files Browse the repository at this point in the history
Work Item: 2506
  • Loading branch information
bricelam committed Nov 7, 2014
1 parent 80033a1 commit c76104c
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 68 deletions.
1 change: 1 addition & 0 deletions src/EFTools/EntityDesign/EntityDesign.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
<Reference Include="Microsoft.WizardFramework" />
<Reference Include="System" />
<Reference Include="System.ComponentModel.DataAnnotations" />
<Reference Include="System.Configuration" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.VisualStudio.DataDesign.Common, Version=$(VisualStudioVersion).0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
<Reference Include="Microsoft.CSharp" />
Expand Down
11 changes: 1 addition & 10 deletions src/EFTools/EntityDesign/VisualStudio/ExecutorWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ public string GetProviderServices(string invariantName)
{
Debug.Assert(!string.IsNullOrWhiteSpace(invariantName), "invariantName is null or empty.");

string providerServicesTypeName = null;
try
{
providerServicesTypeName = Invoke<string>("GetProviderServices", new[] { invariantName });
}
catch
{
}

return providerServicesTypeName;
return Invoke<string>("GetProviderServices", new[] { invariantName });
}

private TResult Invoke<TResult>(string operation, IEnumerable<object> args, object anonymousArguments = null)
Expand Down
41 changes: 32 additions & 9 deletions src/EFTools/EntityDesign/VisualStudio/VsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ namespace Microsoft.Data.Entity.Design.VisualStudio
{
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.Common;
using System.Data.Entity.Core.Common;
using System.Data.Entity.Infrastructure.DependencyResolution;
using System.Data.Entity.Infrastructure.Design;
using System.Data.Entity.SqlServer;
using System.Data.SqlClient;
using System.Diagnostics;
Expand Down Expand Up @@ -38,6 +40,7 @@ namespace Microsoft.Data.Entity.Design.VisualStudio
using VSLangProj80;
using VsWebSite;
using VsWebSite90;
using ConfigurationManager = System.Configuration.ConfigurationManager;
using Constants = EnvDTE.Constants;
using PrjKind = VSLangProj.PrjKind;
using Resources = Microsoft.Data.Entity.Design.Resources;
Expand Down Expand Up @@ -374,13 +377,13 @@ private static void LogStandardTask(string message, string filePath, int lineNum
{
ErrorListHelper.MiscErrorList.AddItem(
new ErrorTask
{
Document = filePath,
Text = message,
Line = lineNumber,
Column = columnNumber,
ErrorCategory = category
});
{
Document = filePath,
Text = message,
Line = lineNumber,
Column = columnNumber,
ErrorCategory = category
});
}

// <summary>
Expand Down Expand Up @@ -2196,14 +2199,34 @@ private static string GetModernProviderTypeNameFromProject(
providerServicesTypeName = context.Executor.GetProviderServices(invariantName);
}
}
catch (Exception ex)
catch
{
Debug.Fail(ex.ToString());
try
{
providerServicesTypeName = GetProviderServicesFromConfig(invariantName, project, serviceProvider);
}
catch (Exception ex)
{
Debug.Fail(ex.ToString());
}
}

return providerServicesTypeName;
}

private static string GetProviderServicesFromConfig(
string invariantName,
Project project,
IServiceProvider serviceProvider)
{
var configurationFile = GetProjectConfigurationFile(project, serviceProvider);
var configuration = ConfigurationManager.OpenMappedExeConfiguration(
new ExeConfigurationFileMap { ExeConfigFilename = configurationFile },
ConfigurationUserLevel.None);

return new AppConfigReader(configuration).GetProviderServices(invariantName);
}

internal static string GetProviderManifestTokenConnected(
IDbDependencyResolver resolver, string providerInvariantName, string providerConnectionString)
{
Expand Down
1 change: 1 addition & 0 deletions src/EntityFramework/EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@
<Compile Include="Infrastructure\Annotations\IndexAttributeExtensions.cs" />
<Compile Include="Infrastructure\DependencyResolution\TransactionContextInitializerResolver.cs" />
<Compile Include="Infrastructure\DependencyResolution\TransactionHandlerResolver.cs" />
<Compile Include="Infrastructure\Design\AppConfigReader.cs" />
<Compile Include="Infrastructure\Interception\DatabaseLogger.cs" />
<Compile Include="Infrastructure\Interception\DbConfigurationDispatcher.cs" />
<Compile Include="Infrastructure\Interception\DbConfigurationInterceptionContext.cs" />
Expand Down
45 changes: 45 additions & 0 deletions src/EntityFramework/Infrastructure/Design/AppConfigReader.cs
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();
}
}
}
15 changes: 10 additions & 5 deletions src/EntityFramework/Infrastructure/Design/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,23 @@ internal virtual string GetProviderServicesInternal(string invariantName)
{
DebugCheck.NotEmpty(invariantName);

string providerServicesTypeName = null;
DbConfiguration.LoadConfiguration(_assembly);
var dependencyResolver = DbConfiguration.DependencyResolver;

DbProviderServices providerServices = null;
try
{
DbConfiguration.LoadConfiguration(_assembly);
var providerServices = DbConfiguration.DependencyResolver.GetService<DbProviderServices>(invariantName);
providerServicesTypeName = providerServices.GetType().AssemblyQualifiedName;
providerServices = dependencyResolver.GetService<DbProviderServices>(invariantName);
}
catch
{
}
if (providerServices == null)
{
return null;
}

return providerServicesTypeName;
return providerServices.GetType().AssemblyQualifiedName;
}

// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/EntityFramework/Internal/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace System.Data.Entity.Internal
// </summary>
internal class AppConfig
{
private const string EFSectionName = "entityFramework";
public const string EFSectionName = "entityFramework";

private static readonly AppConfig _defaultInstance = new AppConfig();
private readonly KeyValueConfigurationCollection _appSettings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,48 +46,5 @@ public void GetProviderServices_returns_null_when_unknown()
AppDomain.Unload(domain);
}
}

[Fact]
public void GetProviderServices_returns_null_when_no_project_assembly()
{
var domain = AppDomain.CreateDomain("ExecutorWrapperTests", null, AppDomain.CurrentDomain.SetupInformation);
try
{
var executor = new ExecutorWrapper(
domain,
"UnknownProject.dll");

Assert.Null(executor.GetProviderServices("System.Data.SqlClient"));
}
finally
{
AppDomain.Unload(domain);
}
}

[Fact]
public void GetProviderServices_returns_null_when_no_EntityFramework_assembly()
{
var domain = AppDomain.CreateDomain(
"ExecutorWrapperTests",
null,
new AppDomainSetup
{
// NOTE: This will cause assembly resolution for EntityFramework to fail
ApplicationBase = Path.GetTempPath()
});
try
{
var executor = new ExecutorWrapper(
domain,
Path.GetFileName(GetType().Assembly.CodeBase));

Assert.Null(executor.GetProviderServices("System.Data.SqlClient"));
}
finally
{
AppDomain.Unload(domain);
}
}
}
}
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);
}
}
}
1 change: 1 addition & 0 deletions test/EntityFramework/UnitTests/UnitTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@
<Compile Include="Infrastructure\Annotations\IndexAnnotationTests.cs" />
<Compile Include="Infrastructure\DependencyResolution\TransactionContextInitializerResolverTests.cs" />
<Compile Include="Infrastructure\DependencyResolution\TransactionHandlerResolverTests.cs" />
<Compile Include="Infrastructure\Design\AppConfigReaderTests.cs" />
<Compile Include="Infrastructure\Interception\DatabaseLoggerTests.cs" />
<Compile Include="Infrastructure\Interception\DbTransactionDispatcherTests.cs" />
<Compile Include="Infrastructure\Interception\EnlistTransactionInterceptionContextTests.cs" />
Expand Down

0 comments on commit c76104c

Please sign in to comment.