You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a WatchCategoryEntity class that has a collection of WatchCategorySymbolEntity child entities defined using OwnsMany in my EF Core 5 configuration:
public class WatchCategoryEntity : BaseShardEntity, IAggregateRoot
{
// ...
private readonly List<WatchCategorySymbolEntity> _watchCategorySymbols = new();
public IReadOnlyList<WatchCategorySymbolEntity> WatchCategorySymbols => _watchCategorySymbols;
public void DeleteSymbolRange(IEnumerable<string> symbolIsins)
{
_watchCategorySymbols.RemoveAll(s => symbolIsins.Contains(s.SymbolIsin));
}
// ...
}
public class WatchCategorySymbolEntity
{
public string SymbolIsin { get; init; }
}
internal sealed class WatchCategoryConfig : IEntityTypeConfiguration<WatchCategoryEntity>
{
public void Configure(EntityTypeBuilder<WatchCategoryEntity> builder)
{
builder.Property(p => p.Id).HasColumnType("char(32)");
builder.Property(p => p.Name).HasColumnType("nvarchar").IsUnicode().HasMaxLength(200).IsRequired();
builder.Property(p => p.Order).IsRequired(false);
builder.Property(p => p.IsDefault).IsRequired();
builder.OwnsMany(p => p.WatchCategorySymbols,
b =>
{
b.WithOwner().HasForeignKey("WatchCategoryId");
b.ToTable("WatchCategorySymbols");
b.Property("Id").HasColumnType("bigint");
b.Property(i => i.SymbolIsin).HasMaxLength(12);
});
var navigation = builder.Metadata.FindNavigation(nameof(WatchCategoryEntity.WatchCategorySymbols));
navigation.SetPropertyAccessMode(PropertyAccessMode.Field);
}
}
When I call DeleteSymbolRange on a WatchCategoryEntity instance and then call SaveChangesAsync() on my DbContext, EF Core generates multiple delete statements instead of a single SQL statement. I expect EF Core to generate a single SQL statement with a DELETE...IN clause for better performance.
Is there any way to configure EF Core to generate a single delete statement instead of multiple delete statements when deleting child entities with OwnsMany? Or is there any workaround that can achieve the same result?
The text was updated successfully, but these errors were encountered:
HamidEbr
changed the title
OwnsMany generates multiple delete statements instead of a single SQL statement (EF Core 7)
OwnsMany generates multiple delete statements instead of a single SQL statement (EF Core 7.0.4)
Mar 28, 2023
Note that although EF currently generates multiple DELETE statements instead of one, those multiple statements are batched together in a single roundtrip, so the overall perf difference isn't that huge.
I have a WatchCategoryEntity class that has a collection of WatchCategorySymbolEntity child entities defined using OwnsMany in my EF Core 5 configuration:
When I call DeleteSymbolRange on a WatchCategoryEntity instance and then call SaveChangesAsync() on my DbContext, EF Core generates multiple delete statements instead of a single SQL statement. I expect EF Core to generate a single SQL statement with a DELETE...IN clause for better performance.
Is there any way to configure EF Core to generate a single delete statement instead of multiple delete statements when deleting child entities with OwnsMany? Or is there any workaround that can achieve the same result?
The text was updated successfully, but these errors were encountered: