繁体   English   中英

PostgreSQL:42883:运算符不存在:information_schema.sql_identifier + 未知

[英]PostgreSQL: 42883: operator does not exist: information_schema.sql_identifier + unknown

.NET Core 2.2 应用程序与 Npgsql EF Core 2.2.4、Npgsql Nodatime 2.2.4

我正在尝试批量更新记录,实体框架抛出异常42883: operator does not exist: information_schema.sql_identifier + unknown

如果我不使用 BulkUpdate 而是使用

_dbContext.UpdateRange(data)
_dbContext.SaveChanges();

那么一切都很好。 但我想知道为什么它不能与_dbContext.BulkUpdate(data)

以下是我们正在做的事情的净化版本:

public void BulkUpdate(List<AutoAction> data)
{
    try
    {
        _dbContext.BulkUpdate(data);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
        throw;
    }
}

public class AutoAction
{
    public int Id { get; set; }
    public int SettingsId { get; set; }
    public Settings Setting { get; set; }
    public Type ActionType { get; set; }
    public LocalDate DateEffective { get; set; }
    public bool Processed { get; set; }
    public Instant CreatedDateUtc { get; set; }
    public Instant? ProcessedOnUtc { get; set; }
    public Status Status { get; set; }
}

数据库上下文

public DbSet<AutoAction> AutoActions { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.ApplyConfiguration(new AutoActionConfiguration());
    base.OnModelCreating(modelBuilder);
}

自动操作配置.cs

public class AutoActionConfiguration : IEntityTypeConfiguration<AutoAction> 
{
    public void Configure(EntityTypeBuilder<AutoAction> builder)
    {
        builder.ToTable("AutoAction");
        builder.HasKey(x => x.Id);
        builder.Ignore(x => x.Setting);
        builder.Property(x => x.Id).ValueGeneratedOnAdd();
        builder.Property(a => a.Processed).HasColumnType("BOOLEAN");
        builder.Property(a => a.ProcessedOnUtc).HasColumnType("timestamp");
        builder.Property(a => a.CreatedDateUtc).HasColumnType("timestamp");
        builder.Property(a => a.DateEffective).HasColumnType("date");

        var actionTypeConverter = new EnumToNumberConverter<Type, int>();
        builder.Property(a => a.ActionType).HasConversion(actionTypeConverter).HasColumnName("ActionType");

        var statusTypeConverter = new EnumToNumberConverter<Status, int>();
        builder.Property(a => a.Status).HasConversion(statusTypeConverter).HasColumnName("Status");
    }
}

启动文件

services.AddDbContext<DbContext>((serviceProvider, options) =>
            {
                var factory = serviceProvider.GetRequiredService<IConnectionFactory>();

                var connection = factory.GetPostgresServerConnection();

                options.UseNpgsql(connection, optionsBuilder =>
                {
                    var coreOptionsBuilder = ((IRelationalDbContextOptionsBuilderInfrastructure)optionsBuilder).OptionsBuilder;

                    var extension = coreOptionsBuilder.Options.FindExtension<NpgsqlNodaTimeOptionsExtension>()
                                    ?? new NpgsqlNodaTimeOptionsExtension();

                    ((IDbContextOptionsBuilderInfrastructure)coreOptionsBuilder).AddOrUpdateExtension(extension);
                });
            });

例外

hint: No operator matches the given name and argument types. You might need to add explicit type casts.

stacktrace: at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 1032
End of stack trace from previous location where exception was thrown ---
   at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming) in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 446
   at Npgsql.NpgsqlDataReader.NextResult() in C:\projects\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 332
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1218
   at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior) in C:\projects\npgsql\src\Npgsql\NpgsqlCommand.cs:line 1130
   at EFCore.BulkExtensions.TableInfo.CheckHasIdentity(DbContext context)
   at EFCore.BulkExtensions.SqlBulkOperation.Merge[T](DbContext context, IList`1 entities, TableInfo tableInfo, OperationType operationType, Action`1 progress)
   at EFCore.BulkExtensions.DbContextBulkExtensions.BulkUpdate[T](DbContext context, IList`1 entities, BulkConfig bulkConfig, Action`1 progress)

SQL: SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1 and TABLE_NAME = 'autoaction' and TABLE_SCHEMA = 'dbo'
   42883: operator does not exist: information_schema.sql_identifier + unknown

不确定在哪里添加显式类型转换,因为它被作为异常提示提到。

我准备该错误的方式是尝试在表 rateautoaction 中找到具有 IDENTITY 属性的列,但无法找到。

似乎这是使用此功能的要求。

我不明白的另一部分是那里的 SQL 显示函数 COLUMNPROPERTY 被调用——但这仅在 SQL Server 上。 但是你说你使用的是PostgreSQL。

您是否在 Postgre 中使用 SQL Server 库? 那可不是个好主意。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM