Skip to content

Commit 4722fe3

Browse files
authored
Add a convention that builds a slim model to be used at runtime
Allow the design-time model to be accessed when necessary Fixes #8258
1 parent 20e2cd4 commit 4722fe3

File tree

76 files changed

+1437
-170
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1437
-170
lines changed

Diff for: EFCore.Runtime.slnf

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"test\\EFCore.SqlServer.Tests\\EFCore.SqlServer.Tests.csproj",
3737
"test\\EFCore.Sqlite.FunctionalTests\\EFCore.Sqlite.FunctionalTests.csproj",
3838
"test\\EFCore.Sqlite.Tests\\EFCore.Sqlite.Tests.csproj",
39-
"test\\EFCore.Tests\\EFCore.Tests.csproj",
39+
"test\\EFCore.Tests\\EFCore.Tests.csproj"
4040
]
4141
}
4242
}

Diff for: src/EFCore.Cosmos/Storage/Internal/CosmosDatabaseCreator.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Threading;
66
using System.Threading.Tasks;
77
using Microsoft.EntityFrameworkCore.Cosmos.Internal;
8+
using Microsoft.EntityFrameworkCore.Infrastructure;
89
using Microsoft.EntityFrameworkCore.Metadata;
910
using Microsoft.EntityFrameworkCore.Storage;
1011
using Microsoft.EntityFrameworkCore.Update;
@@ -20,7 +21,7 @@ namespace Microsoft.EntityFrameworkCore.Cosmos.Storage.Internal
2021
public class CosmosDatabaseCreator : IDatabaseCreator
2122
{
2223
private readonly ICosmosClientWrapper _cosmosClient;
23-
private readonly IModel _model;
24+
private readonly IModel _designModel;
2425
private readonly IUpdateAdapterFactory _updateAdapterFactory;
2526
private readonly IDatabase _database;
2627

@@ -32,12 +33,12 @@ public class CosmosDatabaseCreator : IDatabaseCreator
3233
/// </summary>
3334
public CosmosDatabaseCreator(
3435
ICosmosClientWrapper cosmosClient,
35-
IModel model,
36+
ICurrentDbContext context,
3637
IUpdateAdapterFactory updateAdapterFactory,
3738
IDatabase database)
3839
{
3940
_cosmosClient = cosmosClient;
40-
_model = model;
41+
_designModel = context.Context.DesignTimeModel;
4142
_updateAdapterFactory = updateAdapterFactory;
4243
_database = database;
4344
}
@@ -51,7 +52,7 @@ public CosmosDatabaseCreator(
5152
public virtual bool EnsureCreated()
5253
{
5354
var created = _cosmosClient.CreateDatabaseIfNotExists();
54-
foreach (var entityType in _model.GetEntityTypes())
55+
foreach (var entityType in _designModel.GetEntityTypes())
5556
{
5657
var containerName = entityType.GetContainer();
5758
if (containerName != null)
@@ -80,7 +81,7 @@ public virtual async Task<bool> EnsureCreatedAsync(CancellationToken cancellatio
8081
{
8182
var created = await _cosmosClient.CreateDatabaseIfNotExistsAsync(cancellationToken)
8283
.ConfigureAwait(false);
83-
foreach (var entityType in _model.GetEntityTypes())
84+
foreach (var entityType in _designModel.GetEntityTypes())
8485
{
8586
var containerName = entityType.GetContainer();
8687
if (containerName != null)
@@ -130,10 +131,12 @@ public virtual Task SeedAsync(CancellationToken cancellationToken = default)
130131
private IUpdateAdapter AddSeedData()
131132
{
132133
var updateAdapter = _updateAdapterFactory.CreateStandalone();
133-
foreach (var entityType in _model.GetEntityTypes())
134+
foreach (var entityType in _designModel.GetEntityTypes())
134135
{
136+
IEntityType? targetEntityType = null;
135137
foreach (var targetSeed in entityType.GetSeedData())
136138
{
139+
targetEntityType ??= updateAdapter.Model.FindEntityType(entityType.Name)!;
137140
var entry = updateAdapter.CreateEntry(targetSeed, entityType);
138141
entry.EntityState = EntityState.Added;
139142
}

Diff for: src/EFCore.Design/Design/DesignTimeServiceCollectionExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public static IServiceCollection AddDbContextDesignTimeServices(
120120
.AddTransient(_ => context.GetService<IMigrationsModelDiffer>())
121121
.AddTransient(_ => context.GetService<IMigrator>())
122122
.AddTransient(_ => context.GetService<IRelationalTypeMappingSource>())
123-
.AddTransient(_ => context.GetService<IModel>())
123+
.AddTransient(_ => context.DesignTimeModel)
124124
.AddTransient(_ => context.GetService<IModelRuntimeInitializer>());
125125
}
126126
}

Diff for: src/EFCore.Design/Migrations/Internal/SnapshotModelProcessor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public SnapshotModelProcessor(
8484
model = mutableModel.FinalizeModel();
8585
}
8686

87-
return _modelRuntimeInitializer.Initialize((IModel)model, validationLogger: null);
87+
return _modelRuntimeInitializer.Initialize((IModel)model, designTime: true, validationLogger: null);
8888
}
8989

9090
private void ProcessCollection(IEnumerable<IReadOnlyAnnotatable> metadata, string version)

Diff for: src/EFCore.Design/Scaffolding/Internal/RelationalScaffoldingModelFactory.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public virtual IModel Create(DatabaseModel databaseModel, ModelReverseEngineerOp
112112

113113
VisitDatabaseModel(modelBuilder, databaseModel);
114114

115-
return _modelRuntimeInitializer.Initialize(modelBuilder.FinalizeModel(), null);
115+
return _modelRuntimeInitializer.Initialize(modelBuilder.FinalizeModel(), designTime: true, null);
116116
}
117117

118118
/// <summary>

Diff for: src/EFCore.InMemory/Storage/Internal/IInMemoryStore.cs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public interface IInMemoryStore
2525
/// </summary>
2626
bool EnsureCreated(
2727
IUpdateAdapterFactory updateAdapterFactory,
28+
IModel designModel,
2829
IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger);
2930

3031
/// <summary>

Diff for: src/EFCore.InMemory/Storage/Internal/InMemoryDatabase.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
45
using System.Collections.Generic;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Microsoft.EntityFrameworkCore.Diagnostics;
89
using Microsoft.EntityFrameworkCore.Infrastructure;
10+
using Microsoft.EntityFrameworkCore.Metadata;
911
using Microsoft.EntityFrameworkCore.Storage;
1012
using Microsoft.EntityFrameworkCore.Update;
1113
using Microsoft.EntityFrameworkCore.Utilities;
@@ -32,6 +34,7 @@ public class InMemoryDatabase : Database, IInMemoryDatabase
3234
private readonly IInMemoryStore _store;
3335
private readonly IUpdateAdapterFactory _updateAdapterFactory;
3436
private readonly IDiagnosticsLogger<DbLoggerCategory.Update> _updateLogger;
37+
private readonly Func<IModel> _getDesignModel;
3538

3639
/// <summary>
3740
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
@@ -43,6 +46,7 @@ public InMemoryDatabase(
4346
DatabaseDependencies dependencies,
4447
IInMemoryStoreCache storeCache,
4548
IDbContextOptions options,
49+
ICurrentDbContext context,
4650
IUpdateAdapterFactory updateAdapterFactory,
4751
IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger)
4852
: base(dependencies)
@@ -53,6 +57,7 @@ public InMemoryDatabase(
5357
Check.NotNull(updateLogger, nameof(updateLogger));
5458

5559
_store = storeCache.GetStore(options);
60+
_getDesignModel = () => context.Context.DesignTimeModel;
5661
_updateAdapterFactory = updateAdapterFactory;
5762
_updateLogger = updateLogger;
5863
}
@@ -93,6 +98,6 @@ public override Task<int> SaveChangesAsync(
9398
/// doing so can result in application failures when updating to a new Entity Framework Core release.
9499
/// </summary>
95100
public virtual bool EnsureDatabaseCreated()
96-
=> _store.EnsureCreated(_updateAdapterFactory, _updateLogger);
101+
=> _store.EnsureCreated(_updateAdapterFactory, _getDesignModel(), _updateLogger);
97102
}
98103
}

Diff for: src/EFCore.InMemory/Storage/Internal/InMemoryStore.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public virtual InMemoryIntegerValueGenerator<TProperty> GetIntegerValueGenerator
6868
/// </summary>
6969
public virtual bool EnsureCreated(
7070
IUpdateAdapterFactory updateAdapterFactory,
71+
IModel designModel,
7172
IDiagnosticsLogger<DbLoggerCategory.Update> updateLogger)
7273
{
7374
lock (_lock)
@@ -80,11 +81,13 @@ public virtual bool EnsureCreated(
8081

8182
var updateAdapter = updateAdapterFactory.CreateStandalone();
8283
var entries = new List<IUpdateEntry>();
83-
foreach (var entityType in updateAdapter.Model.GetEntityTypes())
84+
foreach (var entityType in designModel.GetEntityTypes())
8485
{
86+
IEntityType? targetEntityType = null;
8587
foreach (var targetSeed in entityType.GetSeedData())
8688
{
87-
var entry = updateAdapter.CreateEntry(targetSeed, entityType);
89+
targetEntityType ??= updateAdapter.Model.FindEntityType(entityType.Name)!;
90+
var entry = updateAdapter.CreateEntry(targetSeed, targetEntityType);
8891
entry.EntityState = EntityState.Added;
8992
entries.Add(entry);
9093
}

Diff for: src/EFCore.Relational/Metadata/Conventions/Infrastructure/RelationalConventionSetBuilder.cs

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ public override ConventionSet CreateConventionSet()
109109
(QueryFilterRewritingConvention)new RelationalQueryFilterRewritingConvention(
110110
Dependencies, RelationalDependencies));
111111

112+
ReplaceConvention(
113+
conventionSet.ModelFinalizedConventions,
114+
(SlimModelConvention)new RelationalSlimModelConvention(Dependencies, RelationalDependencies));
115+
112116
return conventionSet;
113117
}
114118
}

Diff for: src/EFCore.Relational/Metadata/Conventions/Infrastructure/RelationalConventionSetBuilderDependencies.cs

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ public RelationalConventionSetBuilderDependencies(IRelationalAnnotationProvider
7171
/// <summary>
7272
/// The relational annotation provider.
7373
/// </summary>
74-
[Obsolete("This is now part of RelationalModelRuntimeInitializerDependencies")]
7574
public IRelationalAnnotationProvider RelationalAnnotationProvider { get; init; }
7675
}
7776
}

0 commit comments

Comments
 (0)