This repository has been archived by the owner on May 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be2f23d
commit bbfaa6b
Showing
22 changed files
with
252 additions
and
84 deletions.
There are no files selected for viewing
51 changes: 32 additions & 19 deletions
51
...e/20171216094333_InitDatabase.Designer.cs → ...e/20180112095254_InitDatabase.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
3 changes: 1 addition & 2 deletions
3
src/modules/Base/CRMCore.Module.MvcCore/Extensions/ExtensionInfo.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
3 changes: 1 addition & 2 deletions
3
src/modules/Base/CRMCore.Module.MvcCore/Extensions/IExtensionManager.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
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
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
69 changes: 64 additions & 5 deletions
69
src/modules/crm/CRMCore.Module.GraphQL/DbContextExtensions.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 |
---|---|---|
@@ -1,18 +1,77 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Linq; | ||
using System.Linq.Expressions; | ||
using System.Reflection; | ||
|
||
namespace CRMCore.Module.GraphQL | ||
{ | ||
public static class DbContextExtensions | ||
{ | ||
public static IQueryable Query(this DbContext context, string entityName) => | ||
context.Query(context.Model.FindEntityType(entityName).ClrType); | ||
public static IQueryable Query( | ||
this DbContext context, | ||
string entityName) => | ||
context.Query(context.Model.FindEntityType(entityName).ClrType); | ||
|
||
static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set)); | ||
public static IQueryable Query( | ||
this DbContext context, | ||
Type entityType) => | ||
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null); | ||
|
||
public static IQueryable Query( | ||
this DbContext context, | ||
string entityName, | ||
string[] includes) | ||
{ | ||
return context.DbSetQuery( | ||
context.Model.FindEntityType(entityName).ClrType, | ||
includes); | ||
} | ||
|
||
public static IQueryable DbSetQuery( | ||
this DbContext context, | ||
Type entityType, | ||
string[] includes) | ||
{ | ||
return context.Set(entityType, includes); | ||
} | ||
|
||
public static IQueryable Set(this DbContext context, | ||
Type T, | ||
string[] includes) | ||
{ | ||
// Get the generic type definition | ||
MethodInfo method = typeof(DbContext).GetMethod(nameof(DbContext.Set), BindingFlags.Public | BindingFlags.Instance); | ||
|
||
// Build a method with the specific type argument you're interested in | ||
method = method.MakeGenericMethod(T); | ||
|
||
var dbSet = method.Invoke(context, null); | ||
|
||
public static IQueryable Query(this DbContext context, Type entityType) => | ||
(IQueryable)SetMethod.MakeGenericMethod(entityType).Invoke(context, null); | ||
if (includes != null && includes.Count() > 0) | ||
{ | ||
// https://stackoverflow.com/questions/38312437/can-a-string-based-include-alternative-be-created-in-entity-framework-core | ||
MethodInfo includeMethodInfo = typeof(EntityFrameworkQueryableExtensions) | ||
.GetTypeInfo() | ||
.GetDeclaredMethods( | ||
nameof(EntityFrameworkQueryableExtensions.Include)) | ||
.FirstOrDefault(mi => mi.GetParameters().Any(pi => pi.Name == "navigationPropertyPath")); | ||
|
||
foreach (var include in includes) | ||
{ | ||
var parameter = Expression.Parameter(T, "e"); | ||
var property = Expression.PropertyOrField(parameter, include); | ||
includeMethodInfo = includeMethodInfo.MakeGenericMethod(T, property.Type); | ||
|
||
dbSet = includeMethodInfo.Invoke( | ||
null, | ||
new object[] { dbSet, Expression.Lambda(property, new[] { parameter }) }); | ||
} | ||
} | ||
|
||
return dbSet as IQueryable; | ||
} | ||
|
||
static readonly MethodInfo SetMethod = typeof(DbContext).GetMethod(nameof(DbContext.Set)); | ||
} | ||
} |
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
Oops, something went wrong.