Skip to content

Commit 85ab7a4

Browse files
committed
Fixed issue with exceptions being thrown for derivied entities
1 parent bdbfe43 commit 85ab7a4

1 file changed

Lines changed: 16 additions & 18 deletions

File tree

JSONAPI.EntityFramework/DbContextExtensions.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,27 @@ public static class DbContextExtensions
2121
/// <returns></returns>
2222
public static IEnumerable<string> GetKeyNames(this DbContext dbContext, Type type)
2323
{
24-
if (dbContext == null) throw new ArgumentNullException("dbContext");
25-
if (type == null) throw new ArgumentNullException("type");
24+
if (dbContext == null) throw new ArgumentNullException(nameof(dbContext));
25+
if (type == null) throw new ArgumentNullException(nameof(type));
2626

27-
var originalType = type;
28-
29-
while (type != null)
27+
var baseEntityType = type;
28+
while (baseEntityType.BaseType != typeof(Object))
3029
{
31-
var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static);
32-
var method = openMethod.MakeGenericMethod(type);
33-
34-
try
35-
{
36-
return (IEnumerable<string>) method.Invoke(null, new object[] {dbContext});
37-
}
38-
catch (TargetInvocationException)
39-
{
40-
}
30+
baseEntityType = baseEntityType.BaseType;
31+
}
32+
33+
var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static);
34+
var method = openMethod.MakeGenericMethod(baseEntityType);
4135

42-
type = type.BaseType;
36+
try
37+
{
38+
return (IEnumerable<string>) method.Invoke(null, new object[] {dbContext});
39+
}
40+
catch (TargetInvocationException)
41+
{
4342
}
4443

45-
throw new Exception(string.Format("Failed to identify the key names for {0} or any of its parent classes.", originalType.Name));
44+
throw new Exception(string.Format("Failed to identify the key names for {0} or any of its parent classes.", type.Name));
4645
}
4746

4847
/// <summary>
@@ -59,7 +58,6 @@ public static IEnumerable<string> GetKeyNamesFromGeneric<T>(this DbContext dbCon
5958
try
6059
{
6160
objectSet = objectContext.CreateObjectSet<T>();
62-
6361
}
6462
catch (InvalidOperationException e)
6563
{

0 commit comments

Comments
 (0)