From 47abcb73a943213cfd432667c6931877aed06141 Mon Sep 17 00:00:00 2001 From: Vladislav Antonyuk Date: Wed, 25 Aug 2021 21:16:04 +0300 Subject: [PATCH] IdentityProperty override KeyProperty --- .../Config/MicroOrmConfig.cs | 4 ++-- .../SqlGenerator/SqlGenerator.InitProperties.cs | 7 ++++++- .../RepositoriesTests/RepositoriesTests.cs | 2 +- .../SqlGeneratorTests/MsSqlGeneratorTests.cs | 10 +++++----- .../SqlGeneratorTests/MySqlGeneratorTests.cs | 6 +++--- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/MicroOrm.Dapper.Repositories/Config/MicroOrmConfig.cs b/src/MicroOrm.Dapper.Repositories/Config/MicroOrmConfig.cs index ecb989b9..6eb12f2c 100644 --- a/src/MicroOrm.Dapper.Repositories/Config/MicroOrmConfig.cs +++ b/src/MicroOrm.Dapper.Repositories/Config/MicroOrmConfig.cs @@ -23,8 +23,8 @@ public static class MicroOrmConfig public static string TablePrefix { get; set; } /// - /// Use Key attribute as Identity + /// Allow Key attribute as Identity if Identity is not set /// - public static bool UseKeyAsIdentity { get; set; } + public static bool AllowKeyAsIdentity { get; set; } } } diff --git a/src/MicroOrm.Dapper.Repositories/SqlGenerator/SqlGenerator.InitProperties.cs b/src/MicroOrm.Dapper.Repositories/SqlGenerator/SqlGenerator.InitProperties.cs index cecab389..22a0f2e3 100644 --- a/src/MicroOrm.Dapper.Repositories/SqlGenerator/SqlGenerator.InitProperties.cs +++ b/src/MicroOrm.Dapper.Repositories/SqlGenerator/SqlGenerator.InitProperties.cs @@ -39,7 +39,12 @@ private void InitProperties() KeySqlProperties = props.Where(p => p.GetCustomAttributes().Any()).Select(p => new SqlPropertyMetadata(p)).ToArray(); // Use identity as key pattern - var identityProperty = props.FirstOrDefault(p => MicroOrmConfig.UseKeyAsIdentity ? p.GetCustomAttributes().Any() : p.GetCustomAttributes().Any()); + var identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes().Any()); + if (identityProperty == null && MicroOrmConfig.AllowKeyAsIdentity) + { + identityProperty = props.FirstOrDefault(p => p.GetCustomAttributes().Any()); + } + IdentitySqlProperty = identityProperty != null ? new SqlPropertyMetadata(identityProperty) : null; var dateChangedProperty = props.FirstOrDefault(p => p.GetCustomAttributes().Any()); diff --git a/test/MicroOrm.Dapper.Repositories.Tests/RepositoriesTests/RepositoriesTests.cs b/test/MicroOrm.Dapper.Repositories.Tests/RepositoriesTests/RepositoriesTests.cs index 6d498591..ba5c4e7d 100644 --- a/test/MicroOrm.Dapper.Repositories.Tests/RepositoriesTests/RepositoriesTests.cs +++ b/test/MicroOrm.Dapper.Repositories.Tests/RepositoriesTests/RepositoriesTests.cs @@ -20,7 +20,7 @@ protected RepositoriesTests(IDbContext db, ITestOutputHelper testOutputHelper) { _db = db; _testOutputHelper = testOutputHelper; - MicroOrmConfig.UseKeyAsIdentity = false; + MicroOrmConfig.AllowKeyAsIdentity = false; } [Fact] diff --git a/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MsSqlGeneratorTests.cs b/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MsSqlGeneratorTests.cs index 4936a0c5..c95a8bda 100644 --- a/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MsSqlGeneratorTests.cs +++ b/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MsSqlGeneratorTests.cs @@ -218,12 +218,12 @@ public static void BulkInsertOne() [Fact] public static void BulkInsertOneKeyAsIdentity() { - MicroOrmConfig.UseKeyAsIdentity = true; + MicroOrmConfig.AllowKeyAsIdentity = true; ISqlGenerator userSqlGenerator = new SqlGenerator(_sqlConnector, true); var sqlQuery = userSqlGenerator.GetBulkInsert(new List { new AddressKeyAsIdentity() }); Assert.Equal("INSERT INTO [Addresses] ([Street], [CityId]) VALUES (@Street0, @CityId0)", sqlQuery.GetSql()); - MicroOrmConfig.UseKeyAsIdentity = false; + MicroOrmConfig.AllowKeyAsIdentity = false; } [Fact] @@ -373,14 +373,14 @@ public static void InsertQuoMarks() } [Fact] - public void Insert_UseKeyAsIdentity_QuoMarks() + public void Insert_AllowKeyAsIdentity_QuoMarks() { - MicroOrmConfig.UseKeyAsIdentity = true; + MicroOrmConfig.AllowKeyAsIdentity = true; ISqlGenerator userSqlGenerator = new SqlGenerator(_sqlConnector, true); var sqlQuery = userSqlGenerator.GetInsert(new AddressKeyAsIdentity()); Assert.Equal("INSERT INTO [Addresses] ([Street], [CityId]) VALUES (@Street, @CityId) SELECT SCOPE_IDENTITY() AS [Id]", sqlQuery.GetSql()); - MicroOrmConfig.UseKeyAsIdentity = false; + MicroOrmConfig.AllowKeyAsIdentity = false; } [Fact] diff --git a/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MySqlGeneratorTests.cs b/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MySqlGeneratorTests.cs index a381c746..63499592 100644 --- a/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MySqlGeneratorTests.cs +++ b/test/MicroOrm.Dapper.Repositories.Tests/SqlGeneratorTests/MySqlGeneratorTests.cs @@ -103,14 +103,14 @@ public void Insert_QuoMarks() } [Fact] - public void Insert_UseKeyAsIdentity_QuoMarks() + public void Insert_AllowKeyAsIdentity_QuoMarks() { - MicroOrmConfig.UseKeyAsIdentity = true; + MicroOrmConfig.AllowKeyAsIdentity = true; ISqlGenerator userSqlGenerator = new SqlGenerator(_sqlConnector, true); var sqlQuery = userSqlGenerator.GetInsert(new AddressKeyAsIdentity()); Assert.Equal("INSERT INTO `Addresses` (`Street`, `CityId`) VALUES (@Street, @CityId); SELECT CONVERT(LAST_INSERT_ID(), SIGNED INTEGER) AS `Id`", sqlQuery.GetSql()); - MicroOrmConfig.UseKeyAsIdentity = false; + MicroOrmConfig.AllowKeyAsIdentity = false; } [Fact]