AltairCA / EntityFrameworkCore.PostgreSQL.ColumnEncryption

NPGSQL Extension that supports native PostgreSql's [Raw Encryption Functions (encrypt_iv,decrypt_iv with aes-cbc/pad:pkcs)]
15 stars 3 forks source link

Error in DB Context OnModelCreating #1

Closed mstoffel-infraserv closed 2 years ago

mstoffel-infraserv commented 2 years ago

`protected override void OnModelCreating(ModelBuilder modelBuilder) {

        base.OnModelCreating(modelBuilder);

        modelBuilder.HasPostgresExtension("pgcrypto");
        modelBuilder.UseEncryptAttribute("mypassword", EncKeyLength.L128);

}`

It is blowing up on the UseEncryptAttribute line with the below error:

System.NullReferenceException: Object reference not set to an instance of an object.

at AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption.EfExtension.AttributeExtension.UseEncryptAttribute(ModelBuilder builder, String password, EncKeyLength encKeyLength)

mstoffel-infraserv commented 2 years ago

More Info: .Net Core 3.1. Using version 3.1.1.1 of the AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption library.

AltairCA commented 2 years ago

Can you put the full trace here?

mstoffel-infraserv commented 2 years ago

System.NullReferenceException: Object reference not set to an instance of an object.

at AltairCA.EntityFrameworkCore.PostgreSQL.ColumnEncryption.EfExtension.AttributeExtension.UseEncryptAttribute(ModelBuilder builder, String password, EncKeyLength encKeyLength)

at Prisma.LEX.PM.API.Models.ApiDbContext.OnModelCreating(ModelBuilder modelBuilder) in /app/Models/ApiDbContext.cs:line 52

at Microsoft.EntityFrameworkCore.Infrastructure.ModelCustomizer.Customize(ModelBuilder modelBuilder, DbContext context)

at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder)

at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder)

at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()

at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.b__7_3(IServiceProvider p)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.b__0(ServiceProviderEngineScope scope)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)

at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)

at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)

at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)

at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()

at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()

at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()

at Microsoft.EntityFrameworkCore.DbContext.get_Model()

at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityType()

at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.CheckState()

at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.get_EntityQueryable()

at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Linq.IQueryable.get_Provider()

at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source, Expression1 predicate)

at Prisma.LEX.PM.API.Controllers.LoginController..ctor(ApiDbContext context) in /app/Controllers/LoginController.cs:line 25

at lambda_method(Closure , IServiceProvider , Object[] )

at Microsoft.AspNetCore.Mvc.Controllers.ControllerActivatorProvider.<>c__DisplayClass4_0.b__0(ControllerContext controllerContext)

at Microsoft.AspNetCore.Mvc.Controllers.ControllerFactoryProvider.<>c__DisplayClass5_0.g__CreateController|0(ControllerContext controllerContext)

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 where exception was thrown ---

at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)

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.Server.Kestrel.Core.Internal.Http.HttpProtocol.ProcessRequests[TContext](IHttpApplication`1 application)

mstoffel-infraserv commented 2 years ago

I resolved this issue, and it was not caused by your library. I had 2 properties that weren't marked as NotMapped that were causing this issue, however it wasn't obvious that was the issue due to the stack trace and error received. Thanks!

AltairCA commented 2 years ago

yeah I'm also wondering about that