带有Npgsql EF Core 2.2.4,Npgsql Nodatime 2.2.4的.NET Core 2.2应用程序
我正在尝试批量更新记录,而Entity Framework抛出异常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; }
}
DbContext.cs
public DbSet<AutoAction> AutoActions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AutoActionConfiguration());
base.OnModelCreating(modelBuilder);
}
AutoActionConfiguration.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库吗?那不是一个好主意。
关于c# - PostgreSQL:42883:运算符不存在:information_schema.sql_identifier +未知,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59275916/