Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 16 additions & 18 deletions JSONAPI.EntityFramework/DbContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,27 @@ public static class DbContextExtensions
/// <returns></returns>
public static IEnumerable<string> GetKeyNames(this DbContext dbContext, Type type)
{
if (dbContext == null) throw new ArgumentNullException("dbContext");
if (type == null) throw new ArgumentNullException("type");
if (dbContext == null) throw new ArgumentNullException(nameof(dbContext));
if (type == null) throw new ArgumentNullException(nameof(type));

var originalType = type;

while (type != null)
var baseEntityType = type;
while (baseEntityType.BaseType != typeof(Object))
{
var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static);
var method = openMethod.MakeGenericMethod(type);

try
{
return (IEnumerable<string>) method.Invoke(null, new object[] {dbContext});
}
catch (TargetInvocationException)
{
}
baseEntityType = baseEntityType.BaseType;
}

var openMethod = typeof(DbContextExtensions).GetMethod("GetKeyNamesFromGeneric", BindingFlags.Public | BindingFlags.Static);
var method = openMethod.MakeGenericMethod(baseEntityType);

type = type.BaseType;
try
{
return (IEnumerable<string>) method.Invoke(null, new object[] {dbContext});
}
catch (TargetInvocationException)
{
}

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

/// <summary>
Expand All @@ -59,7 +58,6 @@ public static IEnumerable<string> GetKeyNamesFromGeneric<T>(this DbContext dbCon
try
{
objectSet = objectContext.CreateObjectSet<T>();

}
catch (InvalidOperationException e)
{
Expand Down