Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return void from AddShared and AddOwned #21741

Merged
1 commit merged into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
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
17 changes: 14 additions & 3 deletions src/EFCore/Extensions/ConventionModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -238,8 +238,7 @@ public static bool IsOwned([NotNull] this IConventionModel model, [NotNull] Type
/// <param name="model"> The model to add the owned type to. </param>
/// <param name="clrType"> The type of the entity type that should be owned. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
/// <returns> The name of the owned type. </returns>
public static string AddOwned([NotNull] this IConventionModel model, [NotNull] Type clrType, bool fromDataAnnotation = false)
public static void AddOwned([NotNull] this IConventionModel model, [NotNull] Type clrType, bool fromDataAnnotation = false)
=> Check.NotNull((Model)model, nameof(model)).AddOwned(
Check.NotNull(clrType, nameof(clrType)),
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);
Expand Down Expand Up @@ -300,6 +299,18 @@ public static string AddIgnored([NotNull] this IConventionModel model, [NotNull]
Check.NotNull(clrType, nameof(clrType)),
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Marks the given entity type as shared, indicating that when discovered matching entity types
/// should be configured as shared type entity type.
/// </summary>
/// <param name="model"> The model to add the shared type to. </param>
/// <param name="clrType"> The type of the entity type that should be shared. </param>
/// <param name="fromDataAnnotation"> Indicates whether the configuration was specified using a data annotation. </param>
public static void AddShared([NotNull] this IConventionModel model, [NotNull] Type clrType, bool fromDataAnnotation = false)
=> Check.NotNull((Model)model, nameof(model)).AddShared(
Check.NotNull(clrType, nameof(clrType)),
fromDataAnnotation ? ConfigurationSource.DataAnnotation : ConfigurationSource.Convention);

/// <summary>
/// Forces post-processing on the model such that it is ready for use by the runtime. This post
/// processing happens automatically when using <see cref="DbContext.OnModelCreating" />; this method allows it to be run
Expand Down
15 changes: 12 additions & 3 deletions src/EFCore/Extensions/MutableModelExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
Expand Down Expand Up @@ -227,8 +227,7 @@ public static bool IsOwned([NotNull] this IMutableModel model, [NotNull] Type cl
/// </summary>
/// <param name="model"> The model to add the owned type to. </param>
/// <param name="clrType"> The type of the entity type that should be owned. </param>
/// <returns> The name of the ignored type. </returns>
public static string AddOwned([NotNull] this IMutableModel model, [NotNull] Type clrType)
public static void AddOwned([NotNull] this IMutableModel model, [NotNull] Type clrType)
=> Check.NotNull((Model)model, nameof(model)).AddOwned(
Check.NotNull(clrType, nameof(clrType)), ConfigurationSource.Explicit);

Expand All @@ -243,6 +242,16 @@ public static string RemoveOwned([NotNull] this IMutableModel model, [NotNull] T
=> Check.NotNull((Model)model, nameof(model)).RemoveOwned(
Check.NotNull(clrType, nameof(clrType)));

/// <summary>
/// Marks the given entity type as shared, indicating that when discovered matching entity types
/// should be configured as shared type entity type.
/// </summary>
/// <param name="model"> The model to add the shared type to. </param>
/// <param name="clrType"> The type of the entity type that should be shared. </param>
public static void AddShared([NotNull] this IMutableModel model, [NotNull] Type clrType)
=> Check.NotNull((Model)model, nameof(model)).AddShared(
Check.NotNull(clrType, nameof(clrType)), ConfigurationSource.Explicit);

/// <summary>
/// Forces post-processing on the model such that it is ready for use by the runtime. This post
/// processing happens automatically when using <see cref="DbContext.OnModelCreating" />; this method allows it to be run
Expand Down
10 changes: 3 additions & 7 deletions src/EFCore/Metadata/Internal/Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,7 @@ public virtual bool IsOwned([NotNull] Type clrType)
/// 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 virtual string AddOwned([NotNull] Type clrType, ConfigurationSource configurationSource)
public virtual void AddOwned([NotNull] Type clrType, ConfigurationSource configurationSource)
{
var name = GetDisplayName(clrType);
if (!(this[CoreAnnotationNames.OwnedTypes] is Dictionary<string, ConfigurationSource> ownedTypes))
Expand All @@ -860,12 +860,10 @@ public virtual string AddOwned([NotNull] Type clrType, ConfigurationSource confi
if (ownedTypes.TryGetValue(name, out var oldConfigurationSource))
{
ownedTypes[name] = configurationSource.Max(oldConfigurationSource);
return name;
return;
}

ownedTypes.Add(name, configurationSource);

return name;
}

/// <summary>
Expand All @@ -891,7 +889,7 @@ public virtual string RemoveOwned([NotNull] Type clrType)
/// 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 virtual Type AddShared([NotNull] Type clrType, ConfigurationSource configurationSource)
public virtual void AddShared([NotNull] Type clrType, ConfigurationSource configurationSource)
{
if (_entityTypes.Any(et => !et.Value.HasSharedClrType && et.Value.ClrType == clrType))
{
Expand All @@ -906,8 +904,6 @@ public virtual Type AddShared([NotNull] Type clrType, ConfigurationSource config
{
_sharedEntityClrTypes.Add(clrType, configurationSource);
}

return clrType;
}

/// <summary>
Expand Down
6 changes: 5 additions & 1 deletion test/EFCore.Tests/ApiConsistencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,11 @@ public override bool TryGetProviderOptionsDelegate(out Action<DbContextOptionsBu
typeof(ConventionAnnotatableExtensions).GetMethod(nameof(ConventionAnnotatableExtensions.SetOrRemoveAnnotation)),
typeof(ConventionAnnotatableExtensions).GetMethod(nameof(ConventionAnnotatableExtensions.AddAnnotations)),
typeof(MutableAnnotatableExtensions).GetMethod(nameof(MutableAnnotatableExtensions.AddAnnotations)),
typeof(IConventionModel).GetMethod(nameof(IConventionModel.IsShared))
typeof(IConventionModel).GetMethod(nameof(IConventionModel.IsShared)),
typeof(ConventionModelExtensions).GetMethod(nameof(ConventionModelExtensions.AddOwned)),
typeof(ConventionModelExtensions).GetMethod(nameof(ConventionModelExtensions.AddShared)),
typeof(MutableModelExtensions).GetMethod(nameof(ConventionModelExtensions.AddOwned)),
typeof(MutableModelExtensions).GetMethod(nameof(ConventionModelExtensions.AddShared))
};
}
}
Expand Down