From 0bc5d04759bf8bfd9b284bc7227d7d6b2a098bc3 Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Sun, 2 Oct 2022 03:18:36 +0800 Subject: [PATCH] According to convention, if the property is the primary key and it is an integer, make it the identity column. This wasn't being set by default --- .../Internal/JetAnnotationProvider.cs | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/EFCore.Jet/Metadata/Internal/JetAnnotationProvider.cs b/src/EFCore.Jet/Metadata/Internal/JetAnnotationProvider.cs index 9070494..92df733 100644 --- a/src/EFCore.Jet/Metadata/Internal/JetAnnotationProvider.cs +++ b/src/EFCore.Jet/Metadata/Internal/JetAnnotationProvider.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Globalization; using System.Linq; using JetBrains.Annotations; @@ -32,7 +33,7 @@ namespace EntityFrameworkCore.Jet.Metadata.Internal : base(dependencies) { } - + /// /// 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 @@ -86,6 +87,26 @@ namespace EntityFrameworkCore.Jet.Metadata.Internal JetAnnotationNames.Identity, string.Format(CultureInfo.InvariantCulture, "{0}, {1}", seed ?? 1, increment ?? 1)); } + else + { + property = column.PropertyMappings.First().Property; + // Only return auto increment for integer single column primary key + var primaryKey = property.DeclaringEntityType.FindPrimaryKey(); + if (primaryKey != null + && primaryKey.Properties.Count == 1 + && primaryKey.Properties[0] == property + && property.ValueGenerated == ValueGenerated.OnAdd + && property.ClrType.UnwrapNullableType().IsInteger() + && !HasConverter(property)) + { + yield return new Annotation( + JetAnnotationNames.Identity, + string.Format(CultureInfo.InvariantCulture, "{0}, {1}", 1, 1)); + } + } } + + private static bool HasConverter(IProperty property) + => (property.GetValueConverter() ?? property.FindTypeMapping()?.Converter) != null; } } \ No newline at end of file