Search code examples
c#asp.net-coreentity-framework-corenpgsqlnodatime

... is of type 'Instant?' which is not supported by the current database provider


I am using ASP.NET Core WebAPI , .NET 6, EF Core 6, PostgreSQL 14.5 , Visual Studio 2022 Community Edition.

My command

Scaffold-DbContext "Server=127.0.0.1;Port=5432;Database=acc200;User Id=postgres; Password=postgres;" Npgsql.EntityFrameworkCore.PostgreSQL -OutputDir Models -ContextDir Data -Context ApplicationDbContext -force
using System;
using System.Collections.Generic;
using NodaTime;

namespace acc7.Models
{
    /// <summary>
    /// Danh mục tài khoản
    /// </summary>
    public partial class Account
    {
        /// <summary>
        /// PK Tài khoản
        /// </summary>
        public short Id { get; set; }
        /// <summary>
        /// Số hiệu tài khoản
        /// </summary>
        public string AccountNumber { get; set; } = null!;
        /// <summary>
        /// Tên tài khoản
        /// </summary>
        public string AccountName { get; set; } = null!;
        /// <summary>
        /// Tên tài khoản bằng Tiếng Anh
        /// </summary>
        public string? AccountNameEnglish { get; set; }
        /// <summary>
        /// Tên tài khoản bằng Tiếng Trung
        /// </summary>
        public string? AccountNameChinese { get; set; }
        /// <summary>
        /// Tên tài khoản bằng Tiếng Hàn Quốc
        /// </summary>
        public string? AccountNameKorean { get; set; }
        /// <summary>
        /// Diễn giải
        /// </summary>
        public string? Description { get; set; }
        /// <summary>
        /// TK Tổng hợp
        /// </summary>
        public short? ParentId { get; set; }
        public string? InternalCodeId { get; set; }
        /// <summary>
        /// Cấp bậc
        /// </summary>
        public short? Grade { get; set; }
        /// <summary>
        /// Là TK tổng hợp
        /// </summary>
        public bool IsParent { get; set; }
        /// <summary>
        /// Tính chất tài khoản: 0: Dư nợ; 1: Dư có; 2: Lưỡng tính
        /// </summary>
        public short AccountCategoryKind { get; set; }
        /// <summary>
        /// Có hạch toán ngoại tệ
        /// </summary>
        public bool IsPostableInForeignCurrency { get; set; }
        /// <summary>
        /// Chi tiết theo đối tượng
        /// </summary>
        public bool DetailByAccountObject { get; set; }
        /// <summary>
        /// Loại đối tượng: 0 - Nhà cung cấp, 1- Khách hàng, 2- Nhân viên
        /// </summary>
        public short? AccountObjectType { get; set; }
        /// <summary>
        /// Chi tiết theo tài khoản ngân hàng
        /// </summary>
        public bool DetailByBankAccount { get; set; }
        /// <summary>
        /// Chi tiết theo đối tượng tập hợp chi phí
        /// </summary>
        public bool DetailByJob { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByJobKind { get; set; }
        /// <summary>
        /// Chi tiết theo công trình, vụ việc
        /// </summary>
        public bool DetailByProjectWork { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByProjectWorkKind { get; set; }
        /// <summary>
        /// Chi tiết theo đơn hàng
        /// </summary>
        public bool DetailByOrder { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByOrderKind { get; set; }
        /// <summary>
        /// Chi tiết theo hợp đồng
        /// </summary>
        public bool DetailByContract { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByContractKind { get; set; }
        /// <summary>
        /// Chi tiết theo Khoản mục CP
        /// </summary>
        public bool DetailByExpenseItem { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByExpenseItemKind { get; set; }
        /// <summary>
        /// Chi tiết theo đơn vị
        /// </summary>
        public bool DetailByDepartment { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByDepartmentKind { get; set; }
        /// <summary>
        /// Chi tiết theo mã thống kê
        /// </summary>
        public bool DetailByListItem { get; set; }
        /// <summary>
        /// 0 = Chỉ cảnh báo; 1 = Bắt buộc nhập
        /// </summary>
        public short? DetailByListItemKind { get; set; }
        /// <summary>
        /// Trạng thái theo dõi
        /// </summary>
        public bool ActiveStatus { get; set; }
        public Instant? Created { get; set; }
        public string? CreatedBy { get; set; }
        public Instant? Modified { get; set; }
        public string? ModifiedBy { get; set; }
        /// <summary>
        /// Cột dùng để sort trên báo cáo. Không sử dụng trên giao diện.
        /// </summary>
        public string? SortInternalCodeId { get; set; }
        public bool DetailByPuContract { get; set; }
        public short? DetailByPuContractKind { get; set; }
        public short TenantId { get; set; }
    }
}

DB context: https://gist.github.com/donhuvy/3d3ce7e74c3583ab21b5e4ab9a6889c8

error

info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:7000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5221
info: Microsoft.Hosting.Lifetime[0]
      Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
      Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
      Content root path: C:\Users\donhu\source\repos\acc7\
fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      System.InvalidOperationException: The property 'Account.Created' is of type 'Instant?' which is not supported by the current database provider. Either change the property CLR type, or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.
         at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.ValidatePropertyMapping(IModel model, IDiagnosticsLogger`1 logger)
         at Microsoft.EntityFrameworkCore.Infrastructure.ModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
         at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
         at Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure.NpgsqlModelValidator.Validate(IModel model, IDiagnosticsLogger`1 logger)
         at Microsoft.EntityFrameworkCore.Infrastructure.ModelRuntimeInitializer.Initialize(IModel model, Boolean designTime, IDiagnosticsLogger`1 validationLogger)
         at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, ModelCreationDependencies modelCreationDependencies, Boolean designTime)
         at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel(Boolean designTime)
         at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()
         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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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 callSite, 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__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
         at Microsoft.Extensions.DependencyInjection.ServiceProvider.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_ContextServices()
         at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
         at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Internal.IDbContextDependencies.get_StateManager()
         at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.EntryWithoutDetectChanges(TEntity entity)
         at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.Add(TEntity entity)
         at acc7.Services.AuthService.AuthService.RegisterUser(UserDto request) in C:\Users\donhu\source\repos\acc7\Services\AuthService.cs:line 58
         at acc7.Controllers.AuthController.RegisterUser(UserDto request) in C:\Users\donhu\source\repos\acc7\Controllers\AuthController.cs:line 23
         at lambda_method5(Closure , Object )
         at Microsoft.AspNetCore.Mvc.Infrastructure.ActionMethodExecutor.AwaitableObjectResultExecutor.Execute(IActionResultTypeMapper mapper, ObjectMethodExecutor executor, Object controller, Object[] arguments)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeActionMethodAsync>g__Awaited|12_0(ControllerActionInvoker invoker, ValueTask`1 actionResultValueTask)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeNextActionFilterAsync>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.<InvokeFilterPipelineAsync>g__Awaited|20_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
         at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
         at Microsoft.AspNetCore.Authorization.AuthorizationMiddleware.Invoke(HttpContext context)
         at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
         at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)

enter image description here

How to fix it?


Solution

  • You are missing a call to:

    .UseNodaTime();
    

    Inside Program.cs

    builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql("Server=127.0.0.1;Port=5432;Database=acc200;User Id=postgres;Password=postgres;", x => x.UseNodaTime()));