我正在使用.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 ()
birth_date date,
created timestamp with time zone not null,
tenant_id character varying(36)
);
模型
using System;
using System.Collections.Generic;
namespace acc.Models
{
public partial class AccountObject
{
public Guid Id { get; set; }
public DateOnly? BirthDate { get; set; }
public DateTime Created { 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 ...
图片 https://i.imgur.com/tb8uj13.png
如何修复?