Closed moar closed 9 months ago
This happens when there are properties in your DbContext
that are not of type DbSet
that are written above your DbSet
properties
The solution is simple, make sure all DbSet
properties are above all other properties!
Why this happens? because of this line in CoreAdminDataController.cs
, line 96-98
var targetChildListOnDbContext = dbContextObject.GetType().GetProperties()
.FirstOrDefault(p => p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>)
&& p.PropertyType.GetGenericArguments().First().FullName == typeOfChild.PrincipalEntityType.Name);
Specifically, calling p.PropertyType.GetGenericTypeDefinition()
as you iterate through all DbContext
properties
I will fix it in a PR if @edandersen doesn't mind <3
Brilliant! Go nuts pal :-)
From: AbdoMahfoz @.> Sent: Wednesday, November 30, 2022 9:15:55 PM To: edandersen/core-admin @.> Cc: Ed Andersen @.>; Mention @.> Subject: Re: [edandersen/core-admin] InvalidOperationException: This operation is only valid on generic types. (Issue #77)
This happens when there are properties in your DbContext that are not of type DbSet that are written above your DbSet properties The solution is simple, make sure all DbSet properties are above all other properties!
Why this happens? because of this line in CoreAdminDataController.cs, line 96-98
var targetChildListOnDbContext = dbContextObject.GetType().GetProperties() .FirstOrDefault(p => p.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>) && p.PropertyType.GetGenericArguments().First().FullName == typeOfChild.PrincipalEntityType.Name);
Specifically, calling p.PropertyType.GetGenericTypeDefinition() as you iterate through all DbContext properties
I will fix it in a PR if @edandersenhttps://github.com/edandersen doesn't mind <3
— Reply to this email directly, view it on GitHubhttps://github.com/edandersen/core-admin/issues/77#issuecomment-1332058752, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAEF6HAEXRZXPVWFAW246MTWK5APXANCNFSM6AAAAAARR2G2MQ. You are receiving this because you were mentioned.Message ID: @.***>
Hello,
I get this error when trying to Edit a user, which inherits from IdentityUser. This is the error I get:
System.InvalidOperationException: This operation is only valid on generic types. at System.RuntimeType.GetGenericTypeDefinition() at DotNetEd.CoreAdmin.Controllers.CoreAdminDataController.<>cDisplayClass3_1.b__1(PropertyInfo p)
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerableg Awaited|25_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.gAwaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.g AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
1 source, Func
2 predicate, Boolean& found) at DotNetEd.CoreAdmin.Controllers.CoreAdminDataController.GetDbSetValueOrNull(String dbSetName, DbContext& dbContextObject, Type& typeOfEntity, Dictionary2& relationships) at DotNetEd.CoreAdmin.Controllers.CoreAdminDataController.GetEntityFromDbSet(String dbSetName, String id, DbContext& dbContextObject, Type& typeOfEntity, Dictionary
2& relationships) at DotNetEd.CoreAdmin.Controllers.CoreAdminDataController.EditEntity(String dbSetName, String id) at lambda_method245(Closure , Object , Object[] ) at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.SyncActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeActionMethodAsync() at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeNextActionFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Rethrow(ActionExecutedContextSealed context) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted) at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.InvokeInnerFilterAsync() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.