5
5
using System . Threading ;
6
6
using System . Threading . Tasks ;
7
7
using Microsoft . EntityFrameworkCore . Cosmos . Internal ;
8
+ using Microsoft . EntityFrameworkCore . Infrastructure ;
8
9
using Microsoft . EntityFrameworkCore . Metadata ;
9
10
using Microsoft . EntityFrameworkCore . Storage ;
10
11
using Microsoft . EntityFrameworkCore . Update ;
@@ -20,7 +21,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
20
21
public class CosmosDatabaseCreator : IDatabaseCreator
21
22
{
22
23
private readonly ICosmosClientWrapper _cosmosClient ;
23
- private readonly IModel _model ;
24
+ private readonly IModel _designModel ;
24
25
private readonly IUpdateAdapterFactory _updateAdapterFactory ;
25
26
private readonly IDatabase _database ;
26
27
@@ -32,12 +33,12 @@ public class CosmosDatabaseCreator : IDatabaseCreator
32
33
/// </summary>
33
34
public CosmosDatabaseCreator (
34
35
ICosmosClientWrapper cosmosClient ,
35
- IModel model ,
36
+ ICurrentDbContext context ,
36
37
IUpdateAdapterFactory updateAdapterFactory ,
37
38
IDatabase database )
38
39
{
39
40
_cosmosClient = cosmosClient ;
40
- _model = model ;
41
+ _designModel = context . Context . DesignTimeModel ;
41
42
_updateAdapterFactory = updateAdapterFactory ;
42
43
_database = database ;
43
44
}
@@ -51,7 +52,7 @@ public CosmosDatabaseCreator(
51
52
public virtual bool EnsureCreated ( )
52
53
{
53
54
var created = _cosmosClient . CreateDatabaseIfNotExists ( ) ;
54
- foreach ( var entityType in _model . GetEntityTypes ( ) )
55
+ foreach ( var entityType in _designModel . GetEntityTypes ( ) )
55
56
{
56
57
var containerName = entityType . GetContainer ( ) ;
57
58
if ( containerName != null )
@@ -80,7 +81,7 @@ public virtual async Task<bool> EnsureCreatedAsync(CancellationToken cancellatio
80
81
{
81
82
var created = await _cosmosClient . CreateDatabaseIfNotExistsAsync ( cancellationToken )
82
83
. ConfigureAwait ( false ) ;
83
- foreach ( var entityType in _model . GetEntityTypes ( ) )
84
+ foreach ( var entityType in _designModel . GetEntityTypes ( ) )
84
85
{
85
86
var containerName = entityType . GetContainer ( ) ;
86
87
if ( containerName != null )
@@ -130,10 +131,12 @@ public virtual Task SeedAsync(CancellationToken cancellationToken = default)
130
131
private IUpdateAdapter AddSeedData ( )
131
132
{
132
133
var updateAdapter = _updateAdapterFactory . CreateStandalone ( ) ;
133
- foreach ( var entityType in _model . GetEntityTypes ( ) )
134
+ foreach ( var entityType in _designModel . GetEntityTypes ( ) )
134
135
{
136
+ IEntityType ? targetEntityType = null ;
135
137
foreach ( var targetSeed in entityType . GetSeedData ( ) )
136
138
{
139
+ targetEntityType ??= updateAdapter . Model . FindEntityType ( entityType . Name ) ! ;
137
140
var entry = updateAdapter . CreateEntry ( targetSeed , entityType ) ;
138
141
entry . EntityState = EntityState . Added ;
139
142
}
0 commit comments