Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions util/Seeder/Pipeline/BulkCommitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Bit.Infrastructure.EntityFramework.Repositories;
using LinqToDB.Data;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using EfCollection = Bit.Infrastructure.EntityFramework.Models.Collection;
using EfCollectionGroup = Bit.Infrastructure.EntityFramework.Models.CollectionGroup;
using EfCollectionUser = Bit.Infrastructure.EntityFramework.Models.CollectionUser;
Expand All @@ -25,8 +26,9 @@ namespace Bit.Seeder.Pipeline;
/// Each list is cleared after insert so the context is ready for the next pipeline run.
///
/// CollectionUser and CollectionGroup require an explicit table name in BulkCopyOptions because
/// they lack both IEntityTypeConfiguration and .ToTable() mappings in DatabaseContext, so LinqToDB
/// cannot resolve their table names automatically.
/// they lack .ToTable() mappings in DatabaseContext, so LinqToDB cannot resolve their table names
/// automatically. Table names vary by provider β€” SQL Server uses singular names while EF Core-managed
/// providers use pluralized names.
/// </remarks>
/// <seealso cref="SeederContext"/>
/// <seealso cref="RecipeExecutor"/>
Expand All @@ -48,9 +50,11 @@ internal void Commit(SeederContext context)

MapCopyAndClear<Core.Entities.Collection, EfCollection>(context.Collections);

MapCopyAndClear<Core.Entities.CollectionUser, EfCollectionUser>(context.CollectionUsers, nameof(Core.Entities.CollectionUser));
MapCopyAndClear<Core.Entities.CollectionUser, EfCollectionUser>(context.CollectionUsers,
GetTableName<EfCollectionUser>());

MapCopyAndClear<Core.Entities.CollectionGroup, EfCollectionGroup>(context.CollectionGroups, nameof(Core.Entities.CollectionGroup));
MapCopyAndClear<Core.Entities.CollectionGroup, EfCollectionGroup>(context.CollectionGroups,
GetTableName<EfCollectionGroup>());

MapCopyAndClear<Core.Vault.Entities.Folder, EfFolder>(context.Folders);

Expand All @@ -59,6 +63,15 @@ internal void Commit(SeederContext context)
CopyAndClear(context.CollectionCiphers);
}

/// <summary>
/// Resolves the table name for an EF entity type from the EF Core model,
/// falling back to the C# class name for SQL Server.
/// </summary>
private string? GetTableName<TEf>() where TEf : class =>
db.Database.IsSqlServer()
? typeof(TEf).Name
: db.Model.FindEntityType(typeof(TEf))?.GetTableName();

private void MapCopyAndClear<TCore, TEf>(List<TCore> entities, string? tableName = null) where TEf : class
{
if (entities.Count is 0)
Expand Down
Loading