Skip to content

Commit 0cdcce7

Browse files
authored
Fix table metadata collection for keyless entities (#77)
* Fix table metadata collection for keyless entities * Add keyless entity to test context
1 parent aacc5cd commit 0cdcce7

3 files changed

Lines changed: 233 additions & 202 deletions

File tree

Lines changed: 50 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,50 @@
1-
using Microsoft.EntityFrameworkCore;
2-
using Microsoft.EntityFrameworkCore.Infrastructure;
3-
4-
using PhenX.EntityFrameworkCore.BulkInsert.Abstractions;
5-
6-
namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata;
7-
8-
internal sealed class MetadataProvider
9-
{
10-
private Dictionary<Type, Dictionary<Type, TableMetadata>> _tablesPerContext = new();
11-
12-
public TableMetadata GetTableInfo<T>(DbContext context)
13-
{
14-
var tables = GetTables(context);
15-
16-
if (!tables.TryGetValue(typeof(T), out var table))
17-
{
18-
throw new InvalidOperationException($"Cannot find metadata for type '{typeof(T)}'.");
19-
}
20-
21-
return table;
22-
}
23-
24-
private Dictionary<Type, TableMetadata> GetTables(DbContext context)
25-
{
26-
lock (_tablesPerContext)
27-
{
28-
var type = context.GetType();
29-
if (_tablesPerContext.TryGetValue(context.GetType(), out var tables))
30-
{
31-
return tables;
32-
}
33-
34-
var provider = context.GetService<IBulkInsertProvider>();
35-
36-
tables = context.Model.GetEntityTypes()
37-
.GroupBy(x => x.ClrType)
38-
.ToDictionary(
39-
x => x.Key,
40-
x => new TableMetadata(x.First(), provider.SqlDialect));
41-
42-
_tablesPerContext[type] = tables;
43-
44-
return tables;
45-
}
46-
}
47-
}
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Infrastructure;
3+
4+
using PhenX.EntityFrameworkCore.BulkInsert.Abstractions;
5+
6+
namespace PhenX.EntityFrameworkCore.BulkInsert.Metadata;
7+
8+
internal sealed class MetadataProvider
9+
{
10+
private Dictionary<Type, Dictionary<Type, TableMetadata>> _tablesPerContext = new();
11+
12+
public TableMetadata GetTableInfo<T>(DbContext context)
13+
{
14+
var tables = GetTables(context);
15+
16+
if (!tables.TryGetValue(typeof(T), out var table))
17+
{
18+
throw new InvalidOperationException($"Cannot find metadata for type '{typeof(T)}'.");
19+
}
20+
21+
return table;
22+
}
23+
24+
private Dictionary<Type, TableMetadata> GetTables(DbContext context)
25+
{
26+
lock (_tablesPerContext)
27+
{
28+
var type = context.GetType();
29+
if (_tablesPerContext.TryGetValue(context.GetType(), out var tables))
30+
{
31+
return tables;
32+
}
33+
34+
var provider = context.GetService<IBulkInsertProvider>();
35+
36+
tables = context.Model.GetEntityTypes()
37+
// Filter out entities without an associated table
38+
// See also https://learn.microsoft.com/en-us/ef/core/modeling/keyless-entity-types
39+
.Where(x => x.GetTableName() is not null)
40+
.GroupBy(x => x.ClrType)
41+
.ToDictionary(
42+
x => x.Key,
43+
x => new TableMetadata(x.First(), provider.SqlDialect));
44+
45+
_tablesPerContext[type] = tables;
46+
47+
return tables;
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)