Skip to content

Commit 086686b

Browse files
committed
A bit more tweaking on legacy EF support
* Removed #ifdef ENTITIES, which doesn't exist anymore. We have just one Npgsql.dll. * If the user requests legacy EF but didn't install the EntityFrameworkLegacy package/dll, they get an informative exception including the nested exception (is the DLL missing? corrupt?) * If the required members inside Npgsql.EntityFramework.dll can't be found (e.g. Instance) throw an informative exception. This should never happen unless we mess up and release a bad DLL, but still. * Did not understand what the TargetProviderServices property added, so removed for now
1 parent 3af882b commit 086686b

2 files changed

Lines changed: 22 additions & 22 deletions

File tree

Npgsql.EntityFramework/NpgsqlServices.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ public static NpgsqlServices Instance
2323
get { return _instance; }
2424
}
2525

26-
public static Type TargetProviderServices
27-
{
28-
get { return typeof(DbProviderServices); }
29-
}
30-
3126
protected override DbCommandDefinition CreateDbCommandDefinition(DbProviderManifest providerManifest, DbCommandTree commandTree)
3227
{
3328
return CreateCommandDefinition(CreateDbCommand(commandTree));

Npgsql/Npgsql/NpgsqlFactory.cs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,34 +76,39 @@ public override DbConnectionStringBuilder CreateConnectionStringBuilder()
7676
#region IServiceProvider Members
7777

7878
public object GetService(Type serviceType) {
79-
#if ENTITIES
8079
// In legacy Entity Framework, this is the entry point for obtaining Npgsql's
8180
// implementation of DbProviderServices. We use reflection for all types to
8281
// avoid any dependencies on EF stuff in this project.
8382

84-
if (serviceType != null && serviceType.Name == "DbProviderServices") {
85-
// User has requested a legacy EF DbProviderServices implementation. Attempt to
86-
// find the Npgsql.EntityFrameworkLegacy assembly
87-
AssemblyName assemblyName = typeof(NpgsqlFactory).Assembly.GetName();
83+
if (serviceType != null && serviceType.FullName == "System.Data.Common.DbProviderServices")
84+
{
85+
// User has requested a legacy EF DbProviderServices implementation. Check our cache first.
86+
if (_legacyEntityFrameworkServices != null)
87+
return _legacyEntityFrameworkServices;
88+
89+
// First time, attempt to find the Npgsql.EntityFrameworkLegacy assembly and load the type via reflection
90+
var assemblyName = typeof(NpgsqlFactory).Assembly.GetName();
8891
assemblyName.Name = "Npgsql.EntityFrameworkLegacy";
89-
Type npgsqlServicesType = npgsqlServicesType = Type.GetType("Npgsql.NpgsqlServices, " + assemblyName.FullName, false);
90-
if (true
91-
&& npgsqlServicesType != null
92-
&& npgsqlServicesType.GetProperty("TargetProviderServices") != null
93-
&& npgsqlServicesType.GetProperty("Instance") != null
94-
) {
95-
// Check if Npgsql.EntityFrameworkLegacy meets user's EF version.
96-
Type dbProviderServicesType = npgsqlServicesType.InvokeMember("TargetProviderServices", BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, null, new object[0]) as Type;
97-
if (serviceType == dbProviderServicesType) {
98-
return npgsqlServicesType.InvokeMember("Instance", BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, null, new object[0]);
99-
}
92+
Assembly npgsqlEfAssembly;
93+
try {
94+
npgsqlEfAssembly = Assembly.Load(assemblyName.FullName);
95+
} catch (Exception e) {
96+
throw new Exception("Could not load Npgsql.EntityFrameworkLegacy assembly, is it installed?", e);
10097
}
98+
99+
Type npgsqlServicesType;
100+
if ((npgsqlServicesType = npgsqlEfAssembly.GetType("Npgsql.NpgsqlServices")) == null ||
101+
npgsqlServicesType.GetProperty("Instance") == null)
102+
throw new Exception("Npgsql.EntityFrameworkLegacy assembly does not seem to contain the correct type!");
103+
104+
return _legacyEntityFrameworkServices = npgsqlServicesType.InvokeMember("Instance", BindingFlags.Public | BindingFlags.Static | BindingFlags.GetProperty, null, null, new object[0]);
101105
}
102-
#endif
103106

104107
return null;
105108
}
106109

110+
private static object _legacyEntityFrameworkServices;
111+
107112
#endregion
108113
}
109114
}

0 commit comments

Comments
 (0)