Skip to content

Commit

Permalink
Block concurrent runtime model initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd committed Feb 20, 2021
1 parent 9b577a4 commit b65cfb4
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/EFCore/Infrastructure/ModelRuntimeInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,35 @@ public virtual IModel Initialize(
{
if (model.ModelDependencies == null)
{
model.ModelDependencies = Dependencies.ModelDependencies;
model = model.GetOrAddRuntimeAnnotationValue(
CoreAnnotationNames.ReadOnlyModel,
static args =>
{
var (initializer, model, validationLogger) = args;
model.ModelDependencies = initializer.Dependencies.ModelDependencies;
InitializeModel(model, preValidation: true);
initializer.InitializeModel(model, preValidation: true);
if (validationLogger != null
&& model is IConventionModel)
{
Dependencies.ModelValidator.Validate(model, validationLogger);
}
if (validationLogger != null
&& model is IConventionModel)
{
initializer.Dependencies.ModelValidator.Validate(model, validationLogger);
}
InitializeModel(model, preValidation: false);
initializer.InitializeModel(model, preValidation: false);
if (model is Model mutableModel)
{
model = mutableModel.OnModelFinalized();
}
if (model is Model mutableModel)
{
model = mutableModel.OnModelFinalized();
}
return model;
},
(this, model, validationLogger));
}
else
{
model = (IModel)model.FindRuntimeAnnotationValue(CoreAnnotationNames.ReadOnlyModel)!;
}

return model;
Expand Down
9 changes: 9 additions & 0 deletions src/EFCore/Metadata/Internal/CoreAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,14 @@ public static class CoreAnnotationNames
/// </summary>
public const string ModelDependencies = "ModelDependencies";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
/// any release. You should only use it directly in your code with extreme caution and knowing that
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
public const string ReadOnlyModel = "ReadOnlyModel";

/// <summary>
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
Expand Down Expand Up @@ -294,6 +302,7 @@ public static class CoreAnnotationNames
EagerLoaded,
ProviderClrType,
ModelDependencies,
ReadOnlyModel,
InverseNavigations,
DerivedTypes,
NavigationCandidates,
Expand Down

0 comments on commit b65cfb4

Please sign in to comment.