我正在使用 .NET 6、ASP.NET Core Web API 6。

資料庫 PostgreSQL 15。
DROP TABLE IF EXISTS account_object;
CREATE TABLE account_object
(
id uuid DEFAULT uuid_generate_v4 () primary key,
account_object_code character varying(64) unique not null,
account_object_name character varying(512),
gender smallint,
birth_date date,
birth_place character varying(256),
agreement_salary numeric(10),
salary_coefficient numeric(5, 2),
number_of_dependent smallint,
insurance_salary numeric(10),
bank_account character varying(64),
bank_name character varying(128),
address character varying(256),
account_object_group_list character varying(512),
account_object_group_list_code character varying(512),
company_tax_code character varying(64),
tel character varying(32),
mobile character varying(32),
fax character varying(32),
email_address character varying(128),
website character varying(128),
payment_term_id character varying(36),
max_debt_amount numeric(18, 2),
due_time smallint,
identification_number character varying(64),
issue_date date,
issue_by character varying(128),
country character varying(128),
province_or_city character varying(128),
district character varying(128),
ward_or_commune character varying(128),
account_object_prefix character varying(64),
contact_name character varying(128),
contact_title character varying(128),
contact_mobile character varying(32),
other_contact_mobile character varying(32),
contact_fixed_tel character varying(64),
contact_email character varying(128),
contact_address character varying(256),
is_vendor boolean not null,
is_customer boolean not null,
is_employee boolean not null,
is_organization_entity boolean not null,
active_status boolean not null,
organization_unit_id character varying(36),
branch_id character varying(36),
created timestamp with time zone not null,
created_by character varying(128),
modified timestamp with time zone,
modified_by character varying(64),
receivable_debt_amount numeric(18, 2),
shipping_address character varying(256),
account_object_group_list_name character varying(512),
employee_id character varying(36),
description character varying(256),
bank_branch_name character varying(256),
bank_province_or_city character varying(128),
legal_representative character varying(128),
e_invoice_contact_name character varying(256),
e_invoice_contact_email character varying(256),
e_invoice_contact_address character varying(256),
e_invoice_contact_mobile character varying(20),
employee_type smallint,
tenant_id character varying(36)
);
CREATE INDEX account_object_idx ON account_object (id);
CREATE INDEX account_object_idx2 ON account_object (tenant_id);
COMMENT ON TABLE public.account_object IS 'Khách hàng, nhà cung c?p, nhan viên';
COMMENT ON COLUMN public.account_object.id IS 'PK ??i t??ng';
COMMENT ON COLUMN public.account_object.account_object_code IS 'M? ??i t??ng';
COMMENT ON COLUMN public.account_object.account_object_name IS 'Tên ??i t??ng';
COMMENT ON COLUMN public.account_object.gender IS 'Gi?i tính';
COMMENT ON COLUMN public.account_object.birth_date IS 'Ngày sinh';
COMMENT ON COLUMN public.account_object.birth_place IS 'N?i sinh';
COMMENT ON COLUMN public.account_object.agreement_salary IS 'L??ng th?a thu?n';
COMMENT ON COLUMN public.account_object.salary_coefficient IS 'H? s? l??ng';
COMMENT ON COLUMN public.account_object.number_of_dependent IS 'S? ng??i ph? thu?c';
COMMENT ON COLUMN public.account_object.insurance_salary IS 'L??ng ?óng b?o hi?m';
COMMENT ON COLUMN public.account_object.bank_account IS 'Tài kho?n ngan hàng (Là s? TK cá nhan n?u là nhan viên)';
COMMENT ON COLUMN public.account_object.bank_name IS 'Tên ngan hàng';
COMMENT ON COLUMN public.account_object.address IS '??a ch? c?a ??i t??ng';
COMMENT ON COLUMN public.account_object.account_object_group_list IS 'L?u internal_code_id c?a nhóm KH, NCC ???c ch?n, cách nhau b?i d?u ;';
COMMENT ON COLUMN public.account_object.account_object_group_list_code IS 'L?u AccountObjectGroupCode c?a các nhóm KH, NCC ???c ch?n, cách nhau b?i d?u ;';
COMMENT ON COLUMN public.account_object.company_tax_code IS 'M? s? thu?';
COMMENT ON COLUMN public.account_object.tel IS 'S? ?i?n tho?i c? ??nh';
COMMENT ON COLUMN public.account_object.mobile IS 'S? ?i?n tho?i di ??ng';
COMMENT ON COLUMN public.account_object.fax IS 'Fax';
COMMENT ON COLUMN public.account_object.email_address IS '??a ch? Email c?a t? ch?c';
COMMENT ON COLUMN public.account_object.website IS 'Website';
COMMENT ON COLUMN public.account_object.payment_term_id IS '?i?u kho?n thanh toán';
COMMENT ON COLUMN public.account_object.max_debt_amount IS 'S? n? t?i ?a';
COMMENT ON COLUMN public.account_object.due_time IS 'H?n n? ( S? ngày)';
COMMENT ON COLUMN public.account_object.identification_number IS 'S? CMTND c?a ng??i liên h?';
COMMENT ON COLUMN public.account_object.issue_date IS 'Ngày c?p CMTND ng??i liên h?';
COMMENT ON COLUMN public.account_object.issue_by IS 'N?i c?p CMTND ng??i liên h?';
COMMENT ON COLUMN public.account_object.country IS 'Qu?c gia';
COMMENT ON COLUMN public.account_object.province_or_city IS 'T?nh/ Thành ph?';
COMMENT ON COLUMN public.account_object.district IS 'Qu?n/ Huy?n';
COMMENT ON COLUMN public.account_object.ward_or_commune IS 'Ph??ng/X?';
COMMENT ON COLUMN public.account_object.account_object_prefix IS 'X?ng h?';
COMMENT ON COLUMN public.account_object.contact_name IS 'Tên ng??i liên h?';
COMMENT ON COLUMN public.account_object.contact_title IS 'Ch?c v? ng??i liên h? (N?u ??i t??ng là nhan viên thì ?ay chính là ch?c v? m?c ??nh c?a nhan viên ?ó)';
COMMENT ON COLUMN public.account_object.contact_mobile IS 'S? ?i?n tho?i di ??ng c?a ng??i liên h?';
COMMENT ON COLUMN public.account_object.contact_fixed_tel IS '?i?n tho?i c? ??nh ng??i liên h?';
COMMENT ON COLUMN public.account_object.contact_email IS 'Email ng??i liên h?';
COMMENT ON COLUMN public.account_object.is_vendor IS 'Là nhà cung c?p';
COMMENT ON COLUMN public.account_object.is_customer IS 'Là khách hàng';
COMMENT ON COLUMN public.account_object.is_employee IS 'Là cán b? nhan viên';
COMMENT ON COLUMN public.account_object.is_organization_entity IS 'Là t? ch?c';
COMMENT ON COLUMN public.account_object.active_status IS 'Tr?ng thái theo d?i';
COMMENT ON COLUMN public.account_object.organization_unit_id IS '??n v? c?a nhan viên';
COMMENT ON COLUMN public.account_object.branch_id IS 'Chi nhánh';
COMMENT ON COLUMN public.account_object.bank_branch_name IS 'Chi nhánh tài kho?n ngan hàng)';
COMMENT ON COLUMN public.account_object.bank_province_or_city IS 'T?nh/Thành ph? n?i m? tài kho?n ngan hàng';
COMMIT;
模型
using System;
using System.Collections.Generic;
namespace acc.Models
{
/// <summary>
/// Khách hàng, nhà cung c?p, nhan viên
/// </summary>
public partial class AccountObject
{
/// <summary>
/// PK ??i t??ng
/// </summary>
public Guid Id { get; set; }
/// <summary>
/// M? ??i t??ng
/// </summary>
public string AccountObjectCode { get; set; } = null!;
/// <summary>
/// Tên ??i t??ng
/// </summary>
public string? AccountObjectName { get; set; }
/// <summary>
/// Gi?i tính
/// </summary>
public short? Gender { get; set; }
/// <summary>
/// Ngày sinh
/// </summary>
public DateOnly? BirthDate { get; set; }
/// <summary>
/// N?i sinh
/// </summary>
public string? BirthPlace { get; set; }
/// <summary>
/// L??ng th?a thu?n
/// </summary>
public decimal? AgreementSalary { get; set; }
/// <summary>
/// H? s? l??ng
/// </summary>
public decimal? SalaryCoefficient { get; set; }
/// <summary>
/// S? ng??i ph? thu?c
/// </summary>
public short? NumberOfDependent { get; set; }
/// <summary>
/// L??ng ?óng b?o hi?m
/// </summary>
public decimal? InsuranceSalary { get; set; }
/// <summary>
/// Tài kho?n ngan hàng (Là s? TK cá nhan n?u là nhan viên)
/// </summary>
public string? BankAccount { get; set; }
/// <summary>
/// Tên ngan hàng
/// </summary>
public string? BankName { get; set; }
/// <summary>
/// ??a ch? c?a ??i t??ng
/// </summary>
public string? Address { get; set; }
/// <summary>
/// L?u internal_code_id c?a nhóm KH, NCC ???c ch?n, cách nhau b?i d?u ;
/// </summary>
public string? AccountObjectGroupList { get; set; }
/// <summary>
/// L?u AccountObjectGroupCode c?a các nhóm KH, NCC ???c ch?n, cách nhau b?i d?u ;
/// </summary>
public string? AccountObjectGroupListCode { get; set; }
/// <summary>
/// M? s? thu?
/// </summary>
public string? CompanyTaxCode { get; set; }
/// <summary>
/// S? ?i?n tho?i c? ??nh
/// </summary>
public string? Tel { get; set; }
/// <summary>
/// S? ?i?n tho?i di ??ng
/// </summary>
public string? Mobile { get; set; }
/// <summary>
/// Fax
/// </summary>
public string? Fax { get; set; }
/// <summary>
/// ??a ch? Email c?a t? ch?c
/// </summary>
public string? EmailAddress { get; set; }
/// <summary>
/// Website
/// </summary>
public string? Website { get; set; }
/// <summary>
/// ?i?u kho?n thanh toán
/// </summary>
public string? PaymentTermId { get; set; }
/// <summary>
/// S? n? t?i ?a
/// </summary>
public decimal? MaxDebtAmount { get; set; }
/// <summary>
/// H?n n? ( S? ngày)
/// </summary>
public short? DueTime { get; set; }
/// <summary>
/// S? CMTND c?a ng??i liên h?
/// </summary>
public string? IdentificationNumber { get; set; }
/// <summary>
/// Ngày c?p CMTND ng??i liên h?
/// </summary>
public DateOnly? IssueDate { get; set; }
/// <summary>
/// N?i c?p CMTND ng??i liên h?
/// </summary>
public string? IssueBy { get; set; }
/// <summary>
/// Qu?c gia
/// </summary>
public string? Country { get; set; }
/// <summary>
/// T?nh/ Thành ph?
/// </summary>
public string? ProvinceOrCity { get; set; }
/// <summary>
/// Qu?n/ Huy?n
/// </summary>
public string? District { get; set; }
/// <summary>
/// Ph??ng/X?
/// </summary>
public string? WardOrCommune { get; set; }
/// <summary>
/// X?ng h?
/// </summary>
public string? AccountObjectPrefix { get; set; }
/// <summary>
/// Tên ng??i liên h?
/// </summary>
public string? ContactName { get; set; }
/// <summary>
/// Ch?c v? ng??i liên h? (N?u ??i t??ng là nhan viên thì ?ay chính là ch?c v? m?c ??nh c?a nhan viên ?ó)
/// </summary>
public string? ContactTitle { get; set; }
/// <summary>
/// S? ?i?n tho?i di ??ng c?a ng??i liên h?
/// </summary>
public string? ContactMobile { get; set; }
public string? OtherContactMobile { get; set; }
/// <summary>
/// ?i?n tho?i c? ??nh ng??i liên h?
/// </summary>
public string? ContactFixedTel { get; set; }
/// <summary>
/// Email ng??i liên h?
/// </summary>
public string? ContactEmail { get; set; }
public string? ContactAddress { get; set; }
/// <summary>
/// Là nhà cung c?p
/// </summary>
public bool IsVendor { get; set; }
/// <summary>
/// Là khách hàng
/// </summary>
public bool IsCustomer { get; set; }
/// <summary>
/// Là cán b? nhan viên
/// </summary>
public bool IsEmployee { get; set; }
/// <summary>
/// Là t? ch?c
/// </summary>
public bool IsOrganizationEntity { get; set; }
/// <summary>
/// Tr?ng thái theo d?i
/// </summary>
public bool ActiveStatus { get; set; }
/// <summary>
/// ??n v? c?a nhan viên
/// </summary>
public string? OrganizationUnitId { get; set; }
/// <summary>
/// Chi nhánh
/// </summary>
public string? BranchId { get; set; }
public DateTime Created { get; set; }
public string? CreatedBy { get; set; }
public DateTime? Modified { get; set; }
public string? ModifiedBy { get; set; }
public decimal? ReceivableDebtAmount { get; set; }
public string? ShippingAddress { get; set; }
public string? AccountObjectGroupListName { get; set; }
public string? EmployeeId { get; set; }
public string? Description { get; set; }
/// <summary>
/// Chi nhánh tài kho?n ngan hàng)
/// </summary>
public string? BankBranchName { get; set; }
/// <summary>
/// T?nh/Thành ph? n?i m? tài kho?n ngan hàng
/// </summary>
public string? BankProvinceOrCity { get; set; }
public string? LegalRepresentative { get; set; }
public string? EInvoiceContactName { get; set; }
public string? EInvoiceContactEmail { get; set; }
public string? EInvoiceContactAddress { get; set; }
public string? EInvoiceContactMobile { get; set; }
public short? EmployeeType { get; set; }
public string? TenantId { get; set; }
}
}
控制器
using acc.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace acc.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AccountObjectController : ControllerBase
{
private readonly ILogger<AccountObjectController> _logger;
private readonly acc200Context _db;
public AccountObjectController(ILogger<AccountObjectController> logger, acc200Context acc200Context)
{
_logger = logger;
_db = acc200Context;
}
[HttpGet("{tenant_id}")]
public IEnumerable<AccountObject> Get(string tenant_id)
{
return _db.AccountObjects.Where(x => x.TenantId == tenant_id).ToArray();
}
}
}
錯誤
System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported. The unsupported member type is located on type 'System.Nullable`1[System.DateOnly]'. Path: $.BirthDate.
---> System.NotSupportedException: Serialization and deserialization of 'System.DateOnly' instances are not supported.
at System.Text.Json.Serialization.Converters.UnsupportedTypeConverter`1.Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
at System.Text.Json.Serialization.Converters.NullableConverter`1.Write(Utf8JsonWriter writer, Nullable`1 value, JsonSerializerOptions options)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Metadata.JsonPropertyInfo`1.GetMemberAndWriteJson(Object obj, WriteStack& state, Utf8JsonWriter writer)
at System.Text.Json.Serialization.Converters.ObjectDefaultConverter`1.OnTryWrite(Utf8JsonWriter writer, T value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.Converters.ArrayConverter`2.OnWriteResume(Utf8JsonWriter writer, TElement[] array, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonCollectionConverter`2.OnTryWrite(Utf8JsonWriter writer, TCollection value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.TryWrite(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
--- End of inner exception stack trace ---
at System.Text.Json.ThrowHelper.ThrowNotSupportedException(WriteStack& state, NotSupportedException ex)
at System.Text.Json.Serialization.JsonConverter`1.WriteCore(Utf8JsonWriter writer, T& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.Serialization.JsonConverter`1.WriteCoreAsObject(Utf8JsonWriter writer, Object value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteCore[TValue](JsonConverter jsonConverter, Utf8JsonWriter writer, TValue& value, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at System.Text.Json.JsonSerializer.WriteStreamAsync[TValue](Stream utf8Json, TValue value, JsonTypeInfo jsonTypeInfo, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|30_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- 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 Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.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)
HEADERS
=======
Accept: text/plain
Host: localhost:7283
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
:method: GET
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,vi-VN;q=0.8,vi;q=0.7
Referer: https://localhost:7283/swagger/index.html
sec-ch-ua: "Chromium";v="106", "Google Chrome";v="106", "Not;A=Brand";v="99"
DNT: 1
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
圖片https://i.imgur.com/tb8uj13.png
怎么修?
uj5u.com熱心網友回復:
是的,目前不支持 DateOnly 和 TimeOnly 序列化。有一個解決方法
public sealed class DateOnlyJsonConverter : JsonConverter<DateOnly>
{
public override DateOnly Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
return DateOnly.FromDateTime(reader.GetDateTime());
}
public override void Write(Utf8JsonWriter writer, DateOnly value, JsonSerializerOptions options)
{
var isoDate = value.ToString("O");
writer.WriteStringValue(isoDate);
}
}
并確保您添加了新的轉換器
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddJsonOptions(options =>
{
options.JsonSerializerOptions.Converters.Add(new DateOnlyJsonConverter());
});
// ...
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/523672.html
