Skip to content

Commit

Permalink
Move NonNullableConventionState to CoreAnnotationNames (#35359)
Browse files Browse the repository at this point in the history
Co-authored-by: ajcvickers <[email protected]>
Fixes #34996
  • Loading branch information
AndriySvyryd authored Jan 7, 2025
1 parent 2954996 commit cc16006
Show file tree
Hide file tree
Showing 46 changed files with 6,463 additions and 4,552 deletions.
10 changes: 5 additions & 5 deletions src/EFCore/Metadata/Conventions/NonNullableConventionBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics.CodeAnalysis;
using Microsoft.EntityFrameworkCore.Metadata.Internal;

namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;

Expand All @@ -14,8 +15,6 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
/// </remarks>
public abstract class NonNullableConventionBase : IModelFinalizingConvention
{
private const string StateAnnotationName = "NonNullableConventionState";

/// <summary>
/// Creates a new instance of <see cref="NonNullableConventionBase" />.
/// </summary>
Expand Down Expand Up @@ -50,8 +49,8 @@ protected virtual bool TryGetNullabilityInfo(
}

var annotation =
modelBuilder.Metadata.FindAnnotation(StateAnnotationName)
?? modelBuilder.Metadata.AddAnnotation(StateAnnotationName, new NullabilityInfoContext());
modelBuilder.Metadata.FindAnnotation(CoreAnnotationNames.NonNullableConventionState)
?? modelBuilder.Metadata.AddAnnotation(CoreAnnotationNames.NonNullableConventionState, new NullabilityInfoContext());

var nullabilityInfoContext = (NullabilityInfoContext)annotation.Value!;

Expand All @@ -69,5 +68,6 @@ protected virtual bool TryGetNullabilityInfo(
public virtual void ProcessModelFinalizing(
IConventionModelBuilder modelBuilder,
IConventionContext<IConventionModelBuilder> context)
=> modelBuilder.Metadata.RemoveAnnotation(StateAnnotationName);
{
}
}
11 changes: 10 additions & 1 deletion src/EFCore/Metadata/Internal/CoreAnnotationNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,14 @@ public static class CoreAnnotationNames
/// </summary>
public const string UnsafeAccessors = "UnsafeAccessors";

/// <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 NonNullableConventionState = "NonNullableConventionState";

/// <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 @@ -426,6 +434,7 @@ public static class CoreAnnotationNames
JsonValueReaderWriterType,
ElementType,
SkipNavigationBeingConfigured,
UnsafeAccessors
UnsafeAccessors,
NonNullableConventionState
};
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public static class ManyTypesUnsafeAccessors
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<BoolNestedCollection>k__BackingField")]
public static extern ref bool[][] BoolNestedCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_boolReadOnlyCollection")]
public static extern ref List<bool> _boolReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<BoolToStringConverterProperty>k__BackingField")]
public static extern ref bool BoolToStringConverterProperty(CompiledModelTestBase.ManyTypes @this);

Expand Down Expand Up @@ -199,6 +202,9 @@ public static class ManyTypesUnsafeAccessors
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Int32NestedCollection>k__BackingField")]
public static extern ref int[][] Int32NestedCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_int32ReadOnlyCollection")]
public static extern ref List<int> _int32ReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Int64>k__BackingField")]
public static extern ref long Int64(CompiledModelTestBase.ManyTypes @this);

Expand Down Expand Up @@ -412,6 +418,9 @@ public static class ManyTypesUnsafeAccessors
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<StringNestedCollection>k__BackingField")]
public static extern ref string[][] StringNestedCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_stringReadOnlyCollection")]
public static extern ref List<string> _stringReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<StringToBoolConverterProperty>k__BackingField")]
public static extern ref string StringToBoolConverterProperty(CompiledModelTestBase.ManyTypes @this);

Expand Down Expand Up @@ -496,6 +505,9 @@ public static class ManyTypesUnsafeAccessors
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<UInt8Array>k__BackingField")]
public static extern ref byte[] UInt8Array(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_uInt8ReadOnlyCollection")]
public static extern ref List<byte> _uInt8ReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);

[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Uri>k__BackingField")]
public static extern ref Uri Uri(CompiledModelTestBase.ManyTypes @this);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
baseEntityType,
discriminatorProperty: "$type",
discriminatorValue: "ManyTypes",
propertyCount: 166,
propertyCount: 170,
keyCount: 1);

var id = runtimeEntityType.AddProperty(
Expand Down Expand Up @@ -70,6 +70,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<BoolNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var boolReadOnlyCollection = runtimeEntityType.AddProperty(
"BoolReadOnlyCollection",
typeof(IReadOnlyCollection<bool>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_boolReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var boolToStringConverterProperty = runtimeEntityType.AddProperty(
"BoolToStringConverterProperty",
typeof(bool),
Expand Down Expand Up @@ -516,6 +522,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<Int32NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var int32ReadOnlyCollection = runtimeEntityType.AddProperty(
"Int32ReadOnlyCollection",
typeof(IReadOnlyCollection<int>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_int32ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var int64 = runtimeEntityType.AddProperty(
"Int64",
typeof(long),
Expand Down Expand Up @@ -991,6 +1003,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<StringNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var stringReadOnlyCollection = runtimeEntityType.AddProperty(
"StringReadOnlyCollection",
typeof(IReadOnlyCollection<string>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_stringReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var stringToBoolConverterProperty = runtimeEntityType.AddProperty(
"StringToBoolConverterProperty",
typeof(string),
Expand Down Expand Up @@ -1189,6 +1207,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8Array", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<UInt8Array>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var uInt8ReadOnlyCollection = runtimeEntityType.AddProperty(
"UInt8ReadOnlyCollection",
typeof(IReadOnlyCollection<byte>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_uInt8ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var uri = runtimeEntityType.AddProperty(
"Uri",
typeof(Uri),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ protected override void BuildBigModel(ModelBuilder modelBuilder, bool jsonColumn
b.Ignore(e => e.NullableGuidNestedCollection);
b.Ignore(e => e.UInt8NestedCollection);
b.Ignore(e => e.NullableUInt8NestedCollection);
b.Ignore(e => e.IPAddressReadOnlyCollection);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
"Microsoft.EntityFrameworkCore.Scaffolding.CompiledModelTestBase+ManyTypes",
typeof(CompiledModelTestBase.ManyTypes),
baseEntityType,
propertyCount: 258,
propertyCount: 263,
keyCount: 1);

var id = runtimeEntityType.AddProperty(
Expand Down Expand Up @@ -59,6 +59,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<BoolNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var boolReadOnlyCollection = runtimeEntityType.AddProperty(
"BoolReadOnlyCollection",
typeof(IReadOnlyCollection<bool>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_boolReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var boolToStringConverterProperty = runtimeEntityType.AddProperty(
"BoolToStringConverterProperty",
typeof(bool),
Expand Down Expand Up @@ -711,6 +717,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("IPAddressArray", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<IPAddressArray>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var iPAddressReadOnlyCollection = runtimeEntityType.AddProperty(
"IPAddressReadOnlyCollection",
typeof(IReadOnlyCollection<IPAddress>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("IPAddressReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_ipAddressReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var iPAddressToBytesConverterProperty = runtimeEntityType.AddProperty(
"IPAddressToBytesConverterProperty",
typeof(IPAddress),
Expand Down Expand Up @@ -757,6 +769,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<Int32NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var int32ReadOnlyCollection = runtimeEntityType.AddProperty(
"Int32ReadOnlyCollection",
typeof(IReadOnlyCollection<int>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_int32ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var int64 = runtimeEntityType.AddProperty(
"Int64",
typeof(long),
Expand Down Expand Up @@ -1526,6 +1544,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<StringNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var stringReadOnlyCollection = runtimeEntityType.AddProperty(
"StringReadOnlyCollection",
typeof(IReadOnlyCollection<string>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_stringReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var stringToBoolConverterProperty = runtimeEntityType.AddProperty(
"StringToBoolConverterProperty",
typeof(string),
Expand Down Expand Up @@ -1742,6 +1766,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<UInt8NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var uInt8ReadOnlyCollection = runtimeEntityType.AddProperty(
"UInt8ReadOnlyCollection",
typeof(IReadOnlyCollection<byte>),
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_uInt8ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));

var uri = runtimeEntityType.AddProperty(
"Uri",
typeof(Uri),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,15 @@ protected virtual void BuildBigModel(ModelBuilder modelBuilder, bool jsonColumns
b.PrimitiveCollection(e => e.EnumU32AsStringArray).ElementType(b => b.HasConversion<string>());
b.PrimitiveCollection(e => e.EnumU64AsStringArray).ElementType(b => b.HasConversion<string>());

b.Property(e => e.BoolReadOnlyCollection);
b.Property(e => e.UInt8ReadOnlyCollection).HasField("_uInt8ReadOnlyCollection");
b.Property(e => e.Int32ReadOnlyCollection);
b.Property(e => e.StringReadOnlyCollection).HasField("_stringReadOnlyCollection");

b.PrimitiveCollection(e => e.IPAddressReadOnlyCollection)
.ElementType(b => b.HasConversion<string>())
.HasField("_ipAddressReadOnlyCollection");

b.Property(e => e.BoolToStringConverterProperty).HasConversion(new BoolToStringConverter("A", "B"));
b.Property(e => e.BoolToTwoValuesConverterProperty).HasConversion(new BoolToTwoValuesConverter<byte>(0, 1));
b.Property(e => e.BoolToZeroOneConverterProperty).HasConversion<BoolToZeroOneConverter<short>>();
Expand Down Expand Up @@ -911,6 +920,27 @@ public class ManyTypes
public IPAddress?[] NullableIPAddressArray { get; set; } = null!;
public PhysicalAddress?[] NullablePhysicalAddressArray { get; set; } = null!;

private List<bool> _boolReadOnlyCollection = [];
private List<byte> _uInt8ReadOnlyCollection = [];
private List<int> _int32ReadOnlyCollection = [];
private List<string> _stringReadOnlyCollection = [];
private List<IPAddress> _ipAddressReadOnlyCollection = [];

public IReadOnlyCollection<bool> BoolReadOnlyCollection
=> _boolReadOnlyCollection.ToList();

public IReadOnlyCollection<byte> UInt8ReadOnlyCollection
=> _uInt8ReadOnlyCollection.ToList();

public IReadOnlyCollection<int> Int32ReadOnlyCollection
=> _int32ReadOnlyCollection.ToList();

public IReadOnlyCollection<string> StringReadOnlyCollection
=> _stringReadOnlyCollection.ToList();

public IReadOnlyCollection<IPAddress> IPAddressReadOnlyCollection
=> _ipAddressReadOnlyCollection.ToList();

public Enum8 Enum8 { get; set; }
public Enum16 Enum16 { get; set; }
public Enum32 Enum32 { get; set; }
Expand Down
Loading

0 comments on commit cc16006

Please sign in to comment.