Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It doesn't work with .NET 6 #2

Open
NayLinHtke opened this issue Jan 3, 2022 · 3 comments
Open

It doesn't work with .NET 6 #2

NayLinHtke opened this issue Jan 3, 2022 · 3 comments

Comments

@NayLinHtke
Copy link

"System.MissingMethodException: Method not found: 'System.String Microsoft.EntityFrameworkCore.Metadata.IPropertyBase.get_Name()'.
at AspNetCoreHero.EntityFrameworkCore.AuditTrail.AuditableContext.OnBeforeSaveChanges(String userId)
at AspNetCoreHero.EntityFrameworkCore.AuditTrail.AuditableContext.SaveChangesAsync(String userId)
at StoreManager.Infrastructure.DbContexts.ApplicationDbContext.SaveChangesAsync(CancellationToken cancellationToken) in D:\All_Web\Research\StoreManager\StoreManager\StoreManager.Infrastructure\DbContexts\ApplicationDbContext.cs:line 54
at StoreManager.Infrastructure.Repositories.UnitOfWork.Commit(CancellationToken cancellationToken) in D:\All_Web\Research\StoreManager\StoreManager\StoreManager.Infrastructure\Repositories\UnitOfWork.cs:line 24
at StoreManager.Application.Features.Brands.Commands.Create.CreateBrandCommandHandler.Handle(CreateBrandCommand request, CancellationToken cancellationToken) in D:\All_Web\Research\StoreManager\StoreManager\StoreManager.Application\Features\Brands\Commands\Create\CreateBrandCommand.cs:line 36
at MediatR.Pipeline.RequestExceptionProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at MediatR.Pipeline.RequestExceptionProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at MediatR.Pipeline.RequestExceptionActionProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at MediatR.Pipeline.RequestExceptionActionProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at MediatR.Pipeline.RequestPostProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at MediatR.Pipeline.RequestPreProcessorBehavior2.Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate1 next)
at StoreManager.Web.Areas.Catalog.Controllers.BrandController.OnPostCreateOrEdit(Int32 id, BrandViewModel brand) in D:\All_Web\Research\StoreManager\StoreManager\StoreManager.Web\Areas\Catalog\Controllers\BrandController.cs:line 63
at lambda_method492(Closure , Object )
at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.TaskOfActionResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.g__Awaited|10_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
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.g__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.g__Awaited|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.Policy.AuthorizationMiddlewareResultHandler.HandleAsync(RequestDelegate next, HttpContext context, AuthorizationPolicy policy, PolicyAuthorizationResult authorizeResult)
at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
at AspNetCoreHero.ToastNotification.Middlewares.NotyfMiddleware.InvokeAsync(HttpContext context, RequestDelegate next)
at Microsoft.AspNetCore.Builder.UseMiddlewareExtensions.<>c__DisplayClass6_1.<b__1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

@learnwithkawsar
Copy link

Facing Same problem

@RayBuildN22
Copy link

same here

@guisantos
Copy link

guisantos commented May 2, 2023

For me this code worked in .net 6, I only had to update the OnBeforeSaveChanges method.

In the updated code, entry.Metadata.GetTableName() is used to get the table name instead of entry.Entity.GetType().Name. Also, entry.State and entry.Properties are accessed through ChangeTracker.Entries() and property.Metadata is used instead of property.

private List<AuditEntry> OnBeforeSaveChanges(string userId)
{
    ChangeTracker.DetectChanges();
    var auditEntries = new List<AuditEntry>();
    foreach (var entry in ChangeTracker.Entries())
    {
        if (entry.Entity is Audit || entry.State == EntityState.Detached || entry.State == EntityState.Unchanged)
            continue;

        var auditEntry = new AuditEntry(entry);
        auditEntry.TableName = entry.Metadata.GetTableName();
        auditEntry.UserId = userId;
        auditEntries.Add(auditEntry);
        foreach (var property in entry.Properties)
        {
            if (property.IsTemporary)
            {
                auditEntry.TemporaryProperties.Add(property);
                continue;
            }

            string propertyName = property.Metadata.Name;
            if (property.Metadata.IsPrimaryKey())
            {
                auditEntry.KeyValues[propertyName] = property.CurrentValue;
                continue;
            }

            switch (entry.State)
            {
                case EntityState.Added:
                    auditEntry.AuditType = Enums.AuditType.Create;
                    auditEntry.NewValues[propertyName] = property.CurrentValue;
                    break;

                case EntityState.Deleted:
                    auditEntry.AuditType = Enums.AuditType.Delete;
                    auditEntry.OldValues[propertyName] = property.OriginalValue;
                    break;

                case EntityState.Modified:
                    if (property.IsModified)
                    {
                        auditEntry.ChangedColumns.Add(propertyName);
                        auditEntry.AuditType = Enums.AuditType.Update;
                        auditEntry.OldValues[propertyName] = property.OriginalValue;
                        auditEntry.NewValues[propertyName] = property.CurrentValue;
                    }
                    break;
            }
        }
    }
    foreach (var auditEntry in auditEntries.Where(_ => !_.HasTemporaryProperties))
    {
        AuditLogs.Add(auditEntry.ToAudit());
    }
    return auditEntries.Where(_ => _.HasTemporaryProperties).ToList();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants