Skip to content

Commit b9cf406

Browse files
committed
Handle identity annotations for compiled models
Add runtime checks for identity seed/increment on compiled models, update annotation retrieval to avoid table-specific overloads for RuntimeProperty, and ensure complex type properties are annotated with ValueGenerationStrategy.None. Improve type mapping logic and set EF_TEST_REWRITE_BASELINES to 0 in test.runsettings.
1 parent 70ee49d commit b9cf406

5 files changed

Lines changed: 55 additions & 13 deletions

File tree

src/EFCore.Jet/Design/Internal/JetCSharpRuntimeAnnotationCodeGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ public override void Generate(IColumn column, CSharpRuntimeAnnotationCodeGenerat
6969
if (!parameters.IsRuntime)
7070
{
7171
var annotations = parameters.Annotations;
72-
//annotations.Remove(JetAnnotationNames.Identity);
7372
}
7473

7574
base.Generate(column, parameters);

src/EFCore.Jet/Extensions/JetPropertyExtensions.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public static class JetPropertyExtensions
1818
/// <returns> The identity seed. </returns>
1919
public static int? GetJetIdentitySeed(this IReadOnlyProperty property)
2020
{
21+
if (property.DeclaringType.Model is RuntimeModel)
22+
{
23+
throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData);
24+
}
2125
var annotation = property.FindAnnotation(JetAnnotationNames.IdentitySeed);
2226
return (int?)annotation?.Value;
2327
}
@@ -172,8 +176,14 @@ public static void SetJetIdentitySeed(this IMutableRelationalPropertyOverrides o
172176
/// <param name="property"> The property. </param>
173177
/// <returns> The identity increment. </returns>
174178
public static int? GetJetIdentityIncrement(this IReadOnlyProperty property)
175-
=> (int?)property[JetAnnotationNames.IdentityIncrement]
179+
{
180+
if (property.DeclaringType.Model is RuntimeModel)
181+
{
182+
throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData);
183+
}
184+
return (int?)property[JetAnnotationNames.IdentityIncrement]
176185
?? property.DeclaringType.Model.GetJetIdentityIncrement();
186+
}
177187

178188
/// <summary>
179189
/// Returns the identity increment.

src/EFCore.Jet/Metadata/Conventions/JetValueGenerationStrategyConvention.cs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
22

33
using EntityFrameworkCore.Jet.Metadata;
4+
using EntityFrameworkCore.Jet.Metadata.Internal;
45

56
// ReSharper disable once CheckNamespace
67
namespace Microsoft.EntityFrameworkCore.Metadata.Conventions
@@ -75,6 +76,10 @@ public virtual void ProcessModelFinalizing(
7576
property.Builder.HasValueGenerationStrategy(strategy);
7677
}
7778
}
79+
80+
// Complex type properties can never be identity columns; annotate them with None so
81+
// the compiled model and runtime model both carry the annotation for comparison.
82+
SetComplexTypePropertyStrategies(entityType);
7883
}
7984

8085
static bool IsStrategyNoneNeeded(IReadOnlyProperty property, StoreObjectIdentifier storeObject)
@@ -88,12 +93,32 @@ static bool IsStrategyNoneNeeded(IReadOnlyProperty property, StoreObjectIdentifi
8893
var providerClrType = (property.GetValueConverter() ?? property.FindRelationalTypeMapping(storeObject)?.Converter)
8994
?.ProviderClrType.UnwrapNullableType();
9095

91-
return providerClrType != null
92-
&& (providerClrType.IsInteger() || providerClrType == typeof(decimal));
96+
// Fall back to property CLR type when there is no converter (e.g. long? with no HasConversion).
97+
// Type mappings may not yet be applied during ProcessModelFinalizing, so FindRelationalTypeMapping
98+
// returns null and providerClrType would be null even for integer-CLR-typed properties.
99+
var clrType = providerClrType ?? property.ClrType.UnwrapNullableType();
100+
return clrType.IsInteger() || clrType == typeof(decimal);
93101
}
94102

95103
return false;
96104
}
105+
106+
static void SetComplexTypePropertyStrategies(IConventionTypeBase typeBase)
107+
{
108+
foreach (var complexProperty in typeBase.GetComplexProperties())
109+
{
110+
var complexType = complexProperty.ComplexType;
111+
foreach (var property in complexType.GetDeclaredProperties())
112+
{
113+
var declaringTable = property.GetMappedStoreObjects(StoreObjectType.Table).FirstOrDefault();
114+
if (declaringTable.Name != null)
115+
{
116+
property.SetAnnotation(JetAnnotationNames.ValueGenerationStrategy, JetValueGenerationStrategy.None, false);
117+
}
118+
}
119+
SetComplexTypePropertyStrategies(complexType);
120+
}
121+
}
97122
}
98123
}
99124
}

src/EFCore.Jet/Metadata/Internal/JetAnnotationProvider.cs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,6 @@ public override IEnumerable<IAnnotation> For(ITableIndex index, bool designTime)
6464
/// </summary>
6565
public override IEnumerable<IAnnotation> For(IColumn column, bool designTime)
6666
{
67-
//Need to do this in both design and runtime
68-
if (!designTime)
69-
{
70-
yield break;
71-
}
72-
7367
var table = StoreObjectIdentifier.Table(column.Table.Name, column.Table.Schema);
7468
var property = column.PropertyMappings
7569
.Select(m => m.Property)
@@ -78,8 +72,22 @@ public override IEnumerable<IAnnotation> For(IColumn column, bool designTime)
7872
== JetValueGenerationStrategy.IdentityColumn);
7973
if (property != null)
8074
{
81-
var seed = property.GetJetIdentitySeed(table);
82-
var increment = property.GetJetIdentityIncrement(table);
75+
// RuntimeProperty (compiled model) throws on the table-specific overloads since
76+
// per-table overrides aren't stored in the compiled model; fall back to model-level
77+
// using direct annotation access to avoid the RuntimeModel guards on the extensions.
78+
int? seed, increment;
79+
if (property is RuntimeProperty)
80+
{
81+
seed = (int?)property[JetAnnotationNames.IdentitySeed]
82+
?? property.DeclaringType.Model.GetJetIdentitySeed();
83+
increment = (int?)property[JetAnnotationNames.IdentityIncrement]
84+
?? property.DeclaringType.Model.GetJetIdentityIncrement();
85+
}
86+
else
87+
{
88+
seed = property.GetJetIdentitySeed(table);
89+
increment = property.GetJetIdentityIncrement(table);
90+
}
8391

8492
yield return new Annotation(
8593
JetAnnotationNames.Identity,

test/EFCore.Jet.FunctionalTests/test.runsettings

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<RunConfiguration>
55
<EnvironmentVariables>
66
<!-- List of environment variables we want to set-->
7-
<EF_TEST_REWRITE_BASELINES>1</EF_TEST_REWRITE_BASELINES>
7+
<EF_TEST_REWRITE_BASELINES>0</EF_TEST_REWRITE_BASELINES>
88
</EnvironmentVariables>
99
</RunConfiguration>
1010
</RunSettings>

0 commit comments

Comments
 (0)