-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Closed
Closed
SQL Server: cannot batch-insert entities with generated values only that have a computed column#27507
Feature
Copy link
Description
If an entity type has only generated values, trying to insert a number of entities which exceeds Min Batch Size throws:
fail: 2/25/2022 13:51:03.366 CoreEventId.SaveChangesFailed[10000] (Microsoft.EntityFrameworkCore.Update)
An exception occurred in the database while saving changes for context type 'BlogContext'.
Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while saving the entity changes. See the inner exception for details.
---> Microsoft.Data.SqlClient.SqlException (0x80131904): The column "GeneratedOnUpdate" cannot be modified because it is either a computed column or is the result of a UNION operator.
The SQL:
SET NOCOUNT ON;
DECLARE @inserted0 TABLE ([Id] int);
INSERT INTO [WithAllDatabaseGenerated] ([GeneratedOnUpdate])
OUTPUT INSERTED.[Id]
INTO @inserted0
VALUES (DEFAULT),
(DEFAULT),
(DEFAULT),
(DEFAULT);
SELECT [t].[Id], [t].[GeneratedOnUpdate] FROM [WithAllDatabaseGenerated] t
INNER JOIN @inserted0 i ON ([t].[Id] = [i].[Id]);Repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
for (var i = 0; i < 3; i++)
{
ctx.WithAllDatabaseGenerated.Add(new());
}
await ctx.SaveChangesAsync();
public class BlogContext : DbContext
{
public DbSet<WithAllDatabaseGenerated> WithAllDatabaseGenerated { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<WithAllDatabaseGenerated>()
.Property(w => w.GeneratedOnUpdate)
.HasComputedColumnSql("80");
}
}
public class WithAllDatabaseGenerated
{
public int Id { get; set; }
public int GeneratedOnUpdate { get; }
}/cc @AndriySvyryd, discovered while working on #27372.