Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cc16006

Browse files
Move NonNullableConventionState to CoreAnnotationNames (#35359)
Co-authored-by: ajcvickers <[email protected]> Fixes #34996
1 parent 2954996 commit cc16006

File tree

46 files changed

+6463
-4552
lines changed

Some content is hidden

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

46 files changed

+6463
-4552
lines changed

src/EFCore/Metadata/Conventions/NonNullableConventionBase.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5+
using Microsoft.EntityFrameworkCore.Metadata.Internal;
56

67
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
78

@@ -14,8 +15,6 @@ namespace Microsoft.EntityFrameworkCore.Metadata.Conventions;
1415
/// </remarks>
1516
public abstract class NonNullableConventionBase : IModelFinalizingConvention
1617
{
17-
private const string StateAnnotationName = "NonNullableConventionState";
18-
1918
/// <summary>
2019
/// Creates a new instance of <see cref="NonNullableConventionBase" />.
2120
/// </summary>
@@ -50,8 +49,8 @@ protected virtual bool TryGetNullabilityInfo(
5049
}
5150

5251
var annotation =
53-
modelBuilder.Metadata.FindAnnotation(StateAnnotationName)
54-
?? modelBuilder.Metadata.AddAnnotation(StateAnnotationName, new NullabilityInfoContext());
52+
modelBuilder.Metadata.FindAnnotation(CoreAnnotationNames.NonNullableConventionState)
53+
?? modelBuilder.Metadata.AddAnnotation(CoreAnnotationNames.NonNullableConventionState, new NullabilityInfoContext());
5554

5655
var nullabilityInfoContext = (NullabilityInfoContext)annotation.Value!;
5756

@@ -69,5 +68,6 @@ protected virtual bool TryGetNullabilityInfo(
6968
public virtual void ProcessModelFinalizing(
7069
IConventionModelBuilder modelBuilder,
7170
IConventionContext<IConventionModelBuilder> context)
72-
=> modelBuilder.Metadata.RemoveAnnotation(StateAnnotationName);
71+
{
72+
}
7373
}

src/EFCore/Metadata/Internal/CoreAnnotationNames.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,14 @@ public static class CoreAnnotationNames
372372
/// </summary>
373373
public const string UnsafeAccessors = "UnsafeAccessors";
374374

375+
/// <summary>
376+
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
377+
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
378+
/// any release. You should only use it directly in your code with extreme caution and knowing that
379+
/// doing so can result in application failures when updating to a new Entity Framework Core release.
380+
/// </summary>
381+
public const string NonNullableConventionState = "NonNullableConventionState";
382+
375383
/// <summary>
376384
/// This is an internal API that supports the Entity Framework Core infrastructure and not subject to
377385
/// the same compatibility standards as public APIs. It may be changed or removed without notice in
@@ -426,6 +434,7 @@ public static class CoreAnnotationNames
426434
JsonValueReaderWriterType,
427435
ElementType,
428436
SkipNavigationBeingConfigured,
429-
UnsafeAccessors
437+
UnsafeAccessors,
438+
NonNullableConventionState
430439
};
431440
}

test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/ManyTypesEntityType.cs

Lines changed: 890 additions & 654 deletions
Large diffs are not rendered by default.

test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/BigModel/ManyTypesUnsafeAccessors.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ public static class ManyTypesUnsafeAccessors
2525
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<BoolNestedCollection>k__BackingField")]
2626
public static extern ref bool[][] BoolNestedCollection(CompiledModelTestBase.ManyTypes @this);
2727

28+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_boolReadOnlyCollection")]
29+
public static extern ref List<bool> _boolReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);
30+
2831
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<BoolToStringConverterProperty>k__BackingField")]
2932
public static extern ref bool BoolToStringConverterProperty(CompiledModelTestBase.ManyTypes @this);
3033

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

205+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_int32ReadOnlyCollection")]
206+
public static extern ref List<int> _int32ReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);
207+
202208
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Int64>k__BackingField")]
203209
public static extern ref long Int64(CompiledModelTestBase.ManyTypes @this);
204210

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

421+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_stringReadOnlyCollection")]
422+
public static extern ref List<string> _stringReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);
423+
415424
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<StringToBoolConverterProperty>k__BackingField")]
416425
public static extern ref string StringToBoolConverterProperty(CompiledModelTestBase.ManyTypes @this);
417426

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

508+
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_uInt8ReadOnlyCollection")]
509+
public static extern ref List<byte> _uInt8ReadOnlyCollection(CompiledModelTestBase.ManyTypes @this);
510+
499511
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "<Uri>k__BackingField")]
500512
public static extern ref Uri Uri(CompiledModelTestBase.ManyTypes @this);
501513

test/EFCore.Cosmos.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/ManyTypesEntityType.cs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
3232
baseEntityType,
3333
discriminatorProperty: "$type",
3434
discriminatorValue: "ManyTypes",
35-
propertyCount: 166,
35+
propertyCount: 170,
3636
keyCount: 1);
3737

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

73+
var boolReadOnlyCollection = runtimeEntityType.AddProperty(
74+
"BoolReadOnlyCollection",
75+
typeof(IReadOnlyCollection<bool>),
76+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
77+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_boolReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
78+
7379
var boolToStringConverterProperty = runtimeEntityType.AddProperty(
7480
"BoolToStringConverterProperty",
7581
typeof(bool),
@@ -516,6 +522,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
516522
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
517523
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<Int32NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
518524

525+
var int32ReadOnlyCollection = runtimeEntityType.AddProperty(
526+
"Int32ReadOnlyCollection",
527+
typeof(IReadOnlyCollection<int>),
528+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
529+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_int32ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
530+
519531
var int64 = runtimeEntityType.AddProperty(
520532
"Int64",
521533
typeof(long),
@@ -991,6 +1003,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
9911003
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
9921004
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<StringNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
9931005

1006+
var stringReadOnlyCollection = runtimeEntityType.AddProperty(
1007+
"StringReadOnlyCollection",
1008+
typeof(IReadOnlyCollection<string>),
1009+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
1010+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_stringReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
1011+
9941012
var stringToBoolConverterProperty = runtimeEntityType.AddProperty(
9951013
"StringToBoolConverterProperty",
9961014
typeof(string),
@@ -1189,6 +1207,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
11891207
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8Array", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
11901208
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<UInt8Array>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
11911209

1210+
var uInt8ReadOnlyCollection = runtimeEntityType.AddProperty(
1211+
"UInt8ReadOnlyCollection",
1212+
typeof(IReadOnlyCollection<byte>),
1213+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
1214+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_uInt8ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
1215+
11921216
var uri = runtimeEntityType.AddProperty(
11931217
"Uri",
11941218
typeof(Uri),

test/EFCore.Cosmos.FunctionalTests/Scaffolding/CompiledModelCosmosTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ protected override void BuildBigModel(ModelBuilder modelBuilder, bool jsonColumn
331331
b.Ignore(e => e.NullableGuidNestedCollection);
332332
b.Ignore(e => e.UInt8NestedCollection);
333333
b.Ignore(e => e.NullableUInt8NestedCollection);
334+
b.Ignore(e => e.IPAddressReadOnlyCollection);
334335
});
335336
}
336337

test/EFCore.InMemory.FunctionalTests/Scaffolding/Baselines/No_NativeAOT/ManyTypesEntityType.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
2727
"Microsoft.EntityFrameworkCore.Scaffolding.CompiledModelTestBase+ManyTypes",
2828
typeof(CompiledModelTestBase.ManyTypes),
2929
baseEntityType,
30-
propertyCount: 258,
30+
propertyCount: 263,
3131
keyCount: 1);
3232

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

62+
var boolReadOnlyCollection = runtimeEntityType.AddProperty(
63+
"BoolReadOnlyCollection",
64+
typeof(IReadOnlyCollection<bool>),
65+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("BoolReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
66+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_boolReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
67+
6268
var boolToStringConverterProperty = runtimeEntityType.AddProperty(
6369
"BoolToStringConverterProperty",
6470
typeof(bool),
@@ -711,6 +717,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
711717
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("IPAddressArray", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
712718
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<IPAddressArray>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
713719

720+
var iPAddressReadOnlyCollection = runtimeEntityType.AddProperty(
721+
"IPAddressReadOnlyCollection",
722+
typeof(IReadOnlyCollection<IPAddress>),
723+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("IPAddressReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
724+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_ipAddressReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
725+
714726
var iPAddressToBytesConverterProperty = runtimeEntityType.AddProperty(
715727
"IPAddressToBytesConverterProperty",
716728
typeof(IPAddress),
@@ -757,6 +769,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
757769
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
758770
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<Int32NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
759771

772+
var int32ReadOnlyCollection = runtimeEntityType.AddProperty(
773+
"Int32ReadOnlyCollection",
774+
typeof(IReadOnlyCollection<int>),
775+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("Int32ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
776+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_int32ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
777+
760778
var int64 = runtimeEntityType.AddProperty(
761779
"Int64",
762780
typeof(long),
@@ -1526,6 +1544,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
15261544
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringNestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
15271545
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<StringNestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
15281546

1547+
var stringReadOnlyCollection = runtimeEntityType.AddProperty(
1548+
"StringReadOnlyCollection",
1549+
typeof(IReadOnlyCollection<string>),
1550+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("StringReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
1551+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_stringReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
1552+
15291553
var stringToBoolConverterProperty = runtimeEntityType.AddProperty(
15301554
"StringToBoolConverterProperty",
15311555
typeof(string),
@@ -1742,6 +1766,12 @@ public static RuntimeEntityType Create(RuntimeModel model, RuntimeEntityType bas
17421766
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8NestedCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
17431767
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("<UInt8NestedCollection>k__BackingField", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
17441768

1769+
var uInt8ReadOnlyCollection = runtimeEntityType.AddProperty(
1770+
"UInt8ReadOnlyCollection",
1771+
typeof(IReadOnlyCollection<byte>),
1772+
propertyInfo: typeof(CompiledModelTestBase.ManyTypes).GetProperty("UInt8ReadOnlyCollection", BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly),
1773+
fieldInfo: typeof(CompiledModelTestBase.ManyTypes).GetField("_uInt8ReadOnlyCollection", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly));
1774+
17451775
var uri = runtimeEntityType.AddProperty(
17461776
"Uri",
17471777
typeof(Uri),

test/EFCore.Specification.Tests/Scaffolding/CompiledModelTestBase.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,15 @@ protected virtual void BuildBigModel(ModelBuilder modelBuilder, bool jsonColumns
221221
b.PrimitiveCollection(e => e.EnumU32AsStringArray).ElementType(b => b.HasConversion<string>());
222222
b.PrimitiveCollection(e => e.EnumU64AsStringArray).ElementType(b => b.HasConversion<string>());
223223

224+
b.Property(e => e.BoolReadOnlyCollection);
225+
b.Property(e => e.UInt8ReadOnlyCollection).HasField("_uInt8ReadOnlyCollection");
226+
b.Property(e => e.Int32ReadOnlyCollection);
227+
b.Property(e => e.StringReadOnlyCollection).HasField("_stringReadOnlyCollection");
228+
229+
b.PrimitiveCollection(e => e.IPAddressReadOnlyCollection)
230+
.ElementType(b => b.HasConversion<string>())
231+
.HasField("_ipAddressReadOnlyCollection");
232+
224233
b.Property(e => e.BoolToStringConverterProperty).HasConversion(new BoolToStringConverter("A", "B"));
225234
b.Property(e => e.BoolToTwoValuesConverterProperty).HasConversion(new BoolToTwoValuesConverter<byte>(0, 1));
226235
b.Property(e => e.BoolToZeroOneConverterProperty).HasConversion<BoolToZeroOneConverter<short>>();
@@ -911,6 +920,27 @@ public class ManyTypes
911920
public IPAddress?[] NullableIPAddressArray { get; set; } = null!;
912921
public PhysicalAddress?[] NullablePhysicalAddressArray { get; set; } = null!;
913922

923+
private List<bool> _boolReadOnlyCollection = [];
924+
private List<byte> _uInt8ReadOnlyCollection = [];
925+
private List<int> _int32ReadOnlyCollection = [];
926+
private List<string> _stringReadOnlyCollection = [];
927+
private List<IPAddress> _ipAddressReadOnlyCollection = [];
928+
929+
public IReadOnlyCollection<bool> BoolReadOnlyCollection
930+
=> _boolReadOnlyCollection.ToList();
931+
932+
public IReadOnlyCollection<byte> UInt8ReadOnlyCollection
933+
=> _uInt8ReadOnlyCollection.ToList();
934+
935+
public IReadOnlyCollection<int> Int32ReadOnlyCollection
936+
=> _int32ReadOnlyCollection.ToList();
937+
938+
public IReadOnlyCollection<string> StringReadOnlyCollection
939+
=> _stringReadOnlyCollection.ToList();
940+
941+
public IReadOnlyCollection<IPAddress> IPAddressReadOnlyCollection
942+
=> _ipAddressReadOnlyCollection.ToList();
943+
914944
public Enum8 Enum8 { get; set; }
915945
public Enum16 Enum16 { get; set; }
916946
public Enum32 Enum32 { get; set; }

0 commit comments

Comments
 (0)