具有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; }
}
public DbSet<AutoAction> AutoActions { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.ApplyConfiguration(new AutoActionConfiguration());
base.OnModelCreating(modelBuilder);
}
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
不知道在哪里添加显式类型强制转换,因为它是异常的提示。
答案 0 :(得分:1)
我准备解决该错误的方法是,尝试在表rateautoaction中找到具有IDENTITY属性的列,但不能。
似乎是使用此功能的必要条件。
我不了解的另一部分是SQL,其中显示了名为COLUMNPROPERTY的函数-但这仅在SQL Server上。但是您说您正在使用PostgreSQL。
您是否将SQL Server库与Postgre一起使用?那不是一个好主意。