Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
at System.Collections.Generic.List`1.get_Item(Int32 index)
at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
Model Define:
[Table("order_scene")]
public class OrderScene
{
/// <summary>
///
/// </summary>
[Key]
[Column("id")]
public Guid Id { get; set; }
/// <summary>
///
/// </summary>
[Column("body", TypeName = "jsonb")]
// public JObject Body { get; set; }
public string Body { get; set; } //不支持 JObject
/// <summary>
/// 创建时间
/// </summary>
[Column("created_on")]
public DateTime CreatedOn { get; set; } = DateTime.Now;
/// <summary>
/// 修改时间
/// </summary>
[Column("modified_on")]
public DateTime ModifiedOn { get; set; } = DateTime.Now;
/// <summary>
/// 计算列 支付请求日期
/// </summary>
[Column("pay_reqeust_on")]
public DateTime PayRequestOn { get; set; }
/// <summary>
/// 计算列 订单编号
/// </summary>
[Column("code")]
[StringLength(64)]
public string Code { get; set; }
/// <summary>
/// 计算列
/// </summary>
[Column("scene")]
[StringLength(16)]
public string Scene { get; set; }
}
public class OrderSceneConfiguration : IEntityTypeConfiguration<OrderScene>
{
/// <summary>
///
/// </summary>
/// <param name="builder"></param>
public void Configure(EntityTypeBuilder<OrderScene> builder)
{
builder.Property(a => a.Body)
.HasColumnType("jsonb")
.HasComment("json字段");
//builder.HasIndex(a => a.Body)
// .HasMethod("gin");
//builder.HasIndex(a => a.Body)
// .HasMethod("gin");
//生成计算列
builder.Property(a => a.PayRequestOn)
.HasColumnType("timestamp without time zone")
.HasComputedColumnSql("to_timestamp_immutable(body ->> 'payRequestOn')", stored: true)
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Ignore);
builder.Property(a => a.Code)
.HasComputedColumnSql(" body ->> 'code'", stored: true)
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Ignore);
builder.Property(a => a.Scene)
.HasComputedColumnSql(" body ->> 'scene'", stored: true)
.ValueGeneratedOnAddOrUpdate()
.Metadata.SetAfterSaveBehavior(Microsoft.EntityFrameworkCore.Metadata.PropertySaveBehavior.Ignore);
// 索引
builder.HasIndex(a => new { a.CreatedOn, a.Code });
//builder.Property(a => a.CreatedOn)
// .HasColumnType("timestamp without time zone");
builder.Property(a => a.CreatedOn)
.HasColumnType("timestamp without time zone");
builder.Property(a => a.ModifiedOn)
.HasColumnType("timestamp without time zone");
}
}
Migration:
migrationBuilder.CreateTable(
name: "order_scene",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false),
created_on = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
body = table.Column<JObject>(type: "jsonb", nullable: true, comment: "json字段"),
modified_on = table.Column<DateTime>(type: "timestamp without time zone", nullable: false),
pay_reqeust_on = table.Column<DateTime>(type: "timestamp without time zone", nullable: false, computedColumnSql: "to_timestamp_immutable(body ->> 'payRequestOn')", stored: true),
code = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true, computedColumnSql: " body ->> 'code'", stored: true)
},
constraints: table =>
{
table.PrimaryKey("PK_order_scene", x => x.id);
});
migrationBuilder.CreateIndex(
name: "IX_order_scene_created_on_code",
table: "order_scene",
columns: new[] { "created_on", "code" });
table ddl:
CREATE TABLE IF NOT EXISTS public.order_scene_202107
(
id uuid NOT NULL,
created_on timestamp without time zone NOT NULL,
body jsonb,
modified_on timestamp without time zone NOT NULL,
pay_reqeust_on timestamp without time zone GENERATED ALWAYS AS (to_timestamp_immutable((body ->> 'payRequestOn'::text))) STORED,
code character varying(64) COLLATE pg_catalog."default" GENERATED ALWAYS AS ((body ->> 'code'::text)) STORED,
scene character varying(16) COLLATE pg_catalog."default" GENERATED ALWAYS AS ((body ->> 'scene'::text)) STORED,
CONSTRAINT "PK_order_scene_202107" PRIMARY KEY (id)
)
Multiple computed columns SaveChange occurs Exception.
generator execute sql:
Exception:
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index') at System.Collections.Generic.List`1.get_Item(Int32 index) at Npgsql.EntityFrameworkCore.PostgreSQL.Update.Internal.NpgsqlModificationCommandBatch.ConsumeAsync(RelationalDataReader reader, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken) at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
Model Define:
Migration:
table ddl:
The SQL was manually executed successfully: