edandersen / core-admin

Fully automatic admin site CRUD UI generator for ASP.NET Core and .NET 8
Other
561 stars 106 forks source link

InvalidOperationException: This operation is only valid on generic types. #77

Closed moar closed 9 months ago

moar commented 1 year ago

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](IEnumerable1 source, Func2 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, Dictionary2& 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.gAwaited|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.gAwaitRequestTask|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)

AbdoMahfoz commented 1 year 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

edandersen commented 1 year ago

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: @.***>