From abaa7b8a7778090a5233af4ebb2e88c09d1d351d Mon Sep 17 00:00:00 2001 From: Ilya Belyanskiy Date: Tue, 7 Jun 2022 00:05:57 +0300 Subject: [PATCH 1/5] test + rude code --- .../Services/IDbSchemaRetriever.cs | 16 ++++++++ .../Services/Impl/EfCoreDbSchemaRetriever.cs | 37 +++++++++++++++---- .../BinaryExpressionVisitor.cs | 37 ++++++++++++++----- .../Entities/TestEntity.cs | 2 + .../Tests/ConditionGeneratingTests.cs | 21 +++++++++-- 5 files changed, 92 insertions(+), 21 deletions(-) diff --git a/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs b/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs index a6e4b890..73cc890a 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs @@ -30,5 +30,21 @@ public interface IDbSchemaRetriever /// PropertyInfo[] GetPrimaryKeyMembers(Type type); + /// + /// Get info about cases participating in relations between two types. + /// + /// + /// + /// KeyInfo[] GetForeignKeyMembers(Type type, Type type2); + + /// + /// Some type can be overriden, for example Enum can be store as string in the DB. + /// In these cases clr type will be returned from this function. + /// + /// + /// + /// + /// + bool TryGetActualClrType(Type type, MemberInfo memberInfo, out Type clrType); } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs index f31a2f71..06f2b0db 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore.Metadata.Internal; @@ -45,14 +46,8 @@ public string GetColumnName(Type type, MemberInfo memberInfo) { if (!ColumnNamesCache.ContainsKey(memberInfo)) { - var entityType = Model.FindEntityType(type); - - if (entityType == null) - { - throw new InvalidOperationException($"DbSet<{type}> should be added to the DbContext"); - } - - var property = entityType.FindProperty(memberInfo.Name); + var entityType = GetEntityType(type); + var property = GetColumn(type, memberInfo); var identifier = (StoreObjectIdentifier)StoreObjectIdentifier.Create(entityType, StoreObjectType.Table); ColumnNamesCache.Add(memberInfo, property.GetColumnName(identifier)); } @@ -65,6 +60,23 @@ public string GetColumnName(Type type, MemberInfo memberInfo) return columnName; } + private IProperty GetColumn(Type type, MemberInfo memberInfo) + { + return GetEntityType(type).FindProperty(memberInfo.Name); + } + + private IEntityType GetEntityType(Type type) + { + var entityType = Model.FindEntityType(type); + + if (entityType == null) + { + throw new InvalidOperationException($"DbSet<{type}> should be added to the DbContext"); + } + + return entityType; + } + /// public string GetTableName(Type entity) { @@ -139,4 +151,13 @@ public KeyInfo[] GetForeignKeyMembers(Type type, Type type2) return keys; } + + public bool TryGetActualClrType(Type type, MemberInfo memberInfo, out Type clrType) + { + var columnType = GetColumn(type, memberInfo); + + clrType = columnType.FindAnnotation("ProviderClrType")?.Value as Type; + + return clrType is not null; + } } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs index bed0d330..c2c50090 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs @@ -11,12 +11,14 @@ public class BinaryExpressionVisitor : BaseExpressionVisitor { private readonly IExpressionVisitorFactory _factory; private readonly ISqlGenerator _generator; + private readonly IDbSchemaRetriever _schemaRetriever; /// - public BinaryExpressionVisitor(IExpressionVisitorFactory factory, ISqlGenerator generator) + public BinaryExpressionVisitor(IExpressionVisitorFactory factory, ISqlGenerator generator, IDbSchemaRetriever schemaRetriever) { _factory = factory; _generator = generator; + _schemaRetriever = schemaRetriever; } /// @@ -25,21 +27,38 @@ public override SqlBuilder Visit( ArgumentTypes argumentTypes, VisitedMembers visitedMembers) { - // Convert(charValue, Int32) == 122 -> charValue == 'z' if (expression.Left is UnaryExpression { NodeType: ExpressionType.Convert, Operand: MemberExpression memberExpression - } - && memberExpression.Type == typeof(char) + } && expression.Right is ConstantExpression constantExpression) { - var memberSql = _factory.Visit(memberExpression, argumentTypes, visitedMembers); - var constantSql = _factory.Visit(Expression.Constant(Convert.ToChar(constantExpression.Value)), argumentTypes, visitedMembers); + // Convert(enumValue, Int32) == 1 when enum is stores as string -> enumValue == Enum.Value + if (memberExpression.Type.IsEnum && _schemaRetriever.TryGetActualClrType( + memberExpression.Member.DeclaringType, memberExpression.Member, out var clrType) + && clrType == typeof(string)) + { + var valueName = Enum.GetValues(memberExpression.Type) + .Cast() + .First(x => (int)x == (int)constantExpression.Value) + .ToString(); + + var sb = _factory.Visit(memberExpression, argumentTypes, visitedMembers); + sb.Append($" = {_generator.GetSql(valueName)}"); + return sb; + } + + // Convert(charValue, Int32) == 122 -> charValue == 'z' + if (memberExpression.Type == typeof(char)) + { + var memberSql = _factory.Visit(memberExpression, argumentTypes, visitedMembers); + var constantSql = _factory.Visit(Expression.Constant(Convert.ToChar(constantExpression.Value)), argumentTypes, visitedMembers); - return memberSql - .Append(" = ") - .Append(constantSql); + return memberSql + .Append(" = ") + .Append(constantSql); + } } if (expression.Method?.Name == nameof(string.Concat)) diff --git a/tests/Laraue.EfCoreTriggers.Tests/Entities/TestEntity.cs b/tests/Laraue.EfCoreTriggers.Tests/Entities/TestEntity.cs index f7f505fb..b73586f4 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Entities/TestEntity.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Entities/TestEntity.cs @@ -1,4 +1,5 @@ using System; +using Laraue.EfCoreTriggers.Tests.Enums; namespace Laraue.EfCoreTriggers.Tests.Entities { @@ -17,5 +18,6 @@ public class TestEntity public bool BooleanValue { get; set; } public char CharValue { get; set; } + public UserRole EnumValue { get; set; } } } \ No newline at end of file diff --git a/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs b/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs index 389b4ded..d64e0e35 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs @@ -29,11 +29,16 @@ public ConditionGeneratingTests() modelBuilder.Entity() .Property("CharValue"); + modelBuilder.Entity() + .Property("EnumValue") + .HasColumnType("varchar(100)") + .HasConversion(); + _provider = Helper.GetTriggerActionFactory(modelBuilder.Model.FinalizeModel(), collection => collection.AddMySqlServices()); } [Fact] - public virtual void CastingToSameTypeShouldBeIgnored() + public void CastingToSameTypeShouldBeIgnored() { var action = new OnInsertTriggerCondition(user => user.Role > UserRole.Admin); var sql = _provider.Visit(action, new VisitedMembers()); @@ -41,7 +46,7 @@ public virtual void CastingToSameTypeShouldBeIgnored() } [Fact] - public virtual void CastingToAnotherTypeShouldBeTranslated() + public void CastingToAnotherTypeShouldBeTranslated() { var action = new OnInsertTriggerCondition(user => (decimal)user.Role > 50m); var sql = _provider.Visit(action, new VisitedMembers()); @@ -49,7 +54,7 @@ public virtual void CastingToAnotherTypeShouldBeTranslated() } [Fact] - public virtual void ComparisonWithCharTypeShouldProduceCorrectSql() + public void ComparisonWithCharTypeShouldProduceCorrectSql() { var action = new OnInsertTriggerCondition(entity => entity.CharValue == 'D'); var sql = _provider.Visit(action, new VisitedMembers()); @@ -57,7 +62,15 @@ public virtual void ComparisonWithCharTypeShouldProduceCorrectSql() } [Fact] - public virtual void OnlyNotConstantConditionsShouldBeAddedToTrigger() + public void StringEnumShouldGenerateCorrectSql() + { + var action = new OnInsertTriggerCondition(entity => entity.EnumValue == UserRole.Admin); + var sql = _provider.Visit(action, new VisitedMembers()); + Assert.Equal("NEW.EnumValue = 'Admin'", sql); + } + + [Fact] + public void OnlyNotConstantConditionsShouldBeAddedToTrigger() { Assert.Throws(() => new OnUpdateTrigger(TriggerTime.After) From ee3a09fecb10e8bd9cef7dc8f965bdf0cd909c31 Mon Sep 17 00:00:00 2001 From: Ilya Belyanskiy Date: Sat, 16 Jul 2022 17:37:07 +0300 Subject: [PATCH 2/5] added support of string enums --- .../Extensions/ServiceCollectionExtensions.cs | 2 + .../ExpressionVisitorFactory.cs | 11 ++++- .../Impl/ExpressionVisitors/VisitingInfo.cs | 45 +++++++++++++++++++ .../SetMemberInitExpressionVisitor.cs | 9 +++- .../SetNewExpressionVisitor.cs | 12 ++++- .../Services/Impl/SqlGenerator.cs | 17 ++++++- .../MySqlSqlGenerator.cs | 8 +++- .../SqlServerSqlGenerator.cs | 9 ++-- .../NativeMemberAssignmentFunctionsTests.cs | 40 +++++++++++++++++ 9 files changed, 141 insertions(+), 12 deletions(-) create mode 100644 src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/VisitingInfo.cs diff --git a/src/Laraue.EfCoreTriggers.Common/Extensions/ServiceCollectionExtensions.cs b/src/Laraue.EfCoreTriggers.Common/Extensions/ServiceCollectionExtensions.cs index b5d4fd8f..2b24ae09 100644 --- a/src/Laraue.EfCoreTriggers.Common/Extensions/ServiceCollectionExtensions.cs +++ b/src/Laraue.EfCoreTriggers.Common/Extensions/ServiceCollectionExtensions.cs @@ -92,6 +92,8 @@ public static IServiceCollection AddDefaultServices(this IServiceCollection serv .AddMethodCallConverter() + .AddSingleton() + .AddScoped(); } } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/ExpressionVisitorFactory.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/ExpressionVisitorFactory.cs index 41ced501..7ea7c000 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/ExpressionVisitorFactory.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/ExpressionVisitorFactory.cs @@ -27,7 +27,7 @@ public SqlBuilder Visit(Expression expression, ArgumentTypes argumentTypes, Visi { BinaryExpression binary => Visit(binary, argumentTypes, visitedMembers), ConstantExpression constant => Visit(constant, argumentTypes, visitedMembers), - MemberExpression member => Visit(member, argumentTypes, visitedMembers), + MemberExpression member => VisitAndRememberMember(member, argumentTypes, visitedMembers), MethodCallExpression methodCall => Visit(methodCall, argumentTypes, visitedMembers), UnaryExpression unary => Visit(unary, argumentTypes, visitedMembers), NewExpression @new => Visit(@new, argumentTypes, visitedMembers), @@ -35,6 +35,15 @@ public SqlBuilder Visit(Expression expression, ArgumentTypes argumentTypes, Visi _ => throw new NotSupportedException($"Expression of type {expression.GetType()} is not supported") }; } + + private SqlBuilder VisitAndRememberMember(MemberExpression expression, ArgumentTypes argumentTypes, VisitedMembers visitedMembers) + { + var info = _provider.GetRequiredService(); + + return info.ExecuteWithChangingMember( + expression.Member, + () => Visit(expression, argumentTypes, visitedMembers)); + } private SqlBuilder Visit(TExpression expression, ArgumentTypes argumentTypes, VisitedMembers visitedMembers) where TExpression : Expression diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/VisitingInfo.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/VisitingInfo.cs new file mode 100644 index 00000000..e9cf1047 --- /dev/null +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/VisitingInfo.cs @@ -0,0 +1,45 @@ +using System; +using System.Reflection; + +namespace Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; + +/// +/// Information about members visiting. +/// +public class VisitingInfo +{ + /// + /// Current visiting member. + /// + public MemberInfo CurrentMember { get; internal set; } +} + +internal static class VisitingInfoExtensions +{ + /// + /// Change current visiting member info, executes the passed action, + /// sets the previous visiting member info. + /// + /// + /// + /// + /// + /// + internal static T ExecuteWithChangingMember( + this VisitingInfo info, + MemberInfo memberInfo, + Func action) + { + var previousMember = info.CurrentMember; + info.CurrentMember = memberInfo; + + try + { + return action(); + } + finally + { + info.CurrentMember = previousMember; + } + } +} \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetMemberInitExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetMemberInitExpressionVisitor.cs index 80290928..565fdc3e 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetMemberInitExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetMemberInitExpressionVisitor.cs @@ -12,14 +12,17 @@ namespace Laraue.EfCoreTriggers.Common.Services.Impl.SetExpressionVisitors; public class SetMemberInitExpressionVisitor : IMemberInfoVisitor { private readonly IExpressionVisitorFactory _factory; + private readonly VisitingInfo _visitingInfo; /// /// Initializes a new instance of . /// /// - public SetMemberInitExpressionVisitor(IExpressionVisitorFactory factory) + /// + public SetMemberInitExpressionVisitor(IExpressionVisitorFactory factory, VisitingInfo visitingInfo) { _factory = factory; + _visitingInfo = visitingInfo; } /// @@ -29,7 +32,9 @@ public Dictionary Visit(MemberInitExpression expression, { var memberAssignmentExpression = (MemberAssignment)memberBinding; - var sqlExtendedResult = _factory.Visit(memberAssignmentExpression.Expression, argumentTypes, visitedMembers); + var sqlExtendedResult = _visitingInfo.ExecuteWithChangingMember( + memberAssignmentExpression.Member, + () => _factory.Visit(memberAssignmentExpression.Expression, argumentTypes, visitedMembers)); return (memberAssignmentExpression.Member, sqlExtendedResult); }).ToDictionary(x => x.Member, x => x.sqlExtendedResult); diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetNewExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetNewExpressionVisitor.cs index 0dfffbd8..a9da3cfb 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetNewExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SetExpressionVisitors/SetNewExpressionVisitor.cs @@ -12,14 +12,17 @@ namespace Laraue.EfCoreTriggers.Common.Services.Impl.SetExpressionVisitors; public class SetNewExpressionVisitor : IMemberInfoVisitor { private readonly IExpressionVisitorFactory _factory; + private readonly VisitingInfo _visitingInfo; /// /// Initializes a new instance of . /// /// - public SetNewExpressionVisitor(IExpressionVisitorFactory factory) + /// + public SetNewExpressionVisitor(IExpressionVisitorFactory factory, VisitingInfo visitingInfo) { _factory = factory; + _visitingInfo = visitingInfo; } /// @@ -27,6 +30,11 @@ public Dictionary Visit(NewExpression expression, Argume { return expression.Arguments.ToDictionary( argument => ((MemberExpression)argument).Member, - argument => _factory.Visit(argument, argumentTypes, visitedMembers)); + argument => + { + return _visitingInfo.ExecuteWithChangingMember( + ((MemberExpression)argument).Member, + () => _factory.Visit(argument, argumentTypes, visitedMembers)); + }); } } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs index 7ce7c936..25feddab 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Linq.Expressions; using System.Reflection; +using Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; using Laraue.EfCoreTriggers.Common.TriggerBuilders; namespace Laraue.EfCoreTriggers.Common.Services.Impl; @@ -9,6 +10,7 @@ public class SqlGenerator : ISqlGenerator { private readonly IDbSchemaRetriever _adapter; private readonly SqlTypeMappings _sqlTypeMappings; + private readonly VisitingInfo _visitingInfo; public virtual string NewEntityPrefix => "NEW"; @@ -19,10 +21,14 @@ public class SqlGenerator : ISqlGenerator /// protected virtual char Quote => '\''; - public SqlGenerator(IDbSchemaRetriever adapter, SqlTypeMappings sqlTypeMappings) + public SqlGenerator( + IDbSchemaRetriever adapter, + SqlTypeMappings sqlTypeMappings, + VisitingInfo visitingInfo) { _adapter = adapter; _sqlTypeMappings = sqlTypeMappings; + _visitingInfo = visitingInfo; } public virtual string GetOperand(Expression expression) @@ -82,7 +88,14 @@ public string GetSql(char source) public string GetSql(Enum source) { - return source.ToString("D"); + _adapter.TryGetActualClrType( + _visitingInfo.CurrentMember.DeclaringType, + _visitingInfo.CurrentMember, + out var clrType); + + return clrType == typeof(string) + ? GetSql(source.ToString()) + : source.ToString("D"); } public virtual string GetSql(bool source) diff --git a/src/Laraue.EfCoreTriggers.MySql/MySqlSqlGenerator.cs b/src/Laraue.EfCoreTriggers.MySql/MySqlSqlGenerator.cs index 23008c74..9d26780b 100644 --- a/src/Laraue.EfCoreTriggers.MySql/MySqlSqlGenerator.cs +++ b/src/Laraue.EfCoreTriggers.MySql/MySqlSqlGenerator.cs @@ -1,12 +1,16 @@ using Laraue.EfCoreTriggers.Common.Services; using Laraue.EfCoreTriggers.Common.Services.Impl; +using Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; namespace Laraue.EfCoreTriggers.MySql; public class MySqlSqlGenerator : SqlGenerator { - public MySqlSqlGenerator(IDbSchemaRetriever adapter, SqlTypeMappings sqlTypeMappings) - : base(adapter, sqlTypeMappings) + public MySqlSqlGenerator( + IDbSchemaRetriever adapter, + SqlTypeMappings sqlTypeMappings, + VisitingInfo visitingInfo) + : base(adapter, sqlTypeMappings, visitingInfo) { } diff --git a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerSqlGenerator.cs b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerSqlGenerator.cs index f15386a9..531f75a1 100644 --- a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerSqlGenerator.cs +++ b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerSqlGenerator.cs @@ -3,16 +3,19 @@ using System.Reflection; using Laraue.EfCoreTriggers.Common.Services; using Laraue.EfCoreTriggers.Common.Services.Impl; +using Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; using Laraue.EfCoreTriggers.Common.TriggerBuilders; namespace Laraue.EfCoreTriggers.SqlServer; public class SqlServerSqlGenerator : SqlGenerator { - public SqlServerSqlGenerator(IDbSchemaRetriever adapter, SqlTypeMappings sqlTypeMappings) - : base(adapter, sqlTypeMappings) + public SqlServerSqlGenerator( + IDbSchemaRetriever adapter, + SqlTypeMappings sqlTypeMappings, + VisitingInfo visitingInfo) + : base(adapter, sqlTypeMappings, visitingInfo) { - } public override string NewEntityPrefix => "Inserted"; diff --git a/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs b/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs index 23c1742f..cf9c15cf 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs @@ -1,4 +1,5 @@ using System; +using System.Linq.Expressions; using Laraue.EfCoreTriggers.Tests.Infrastructure; using Laraue.EfCoreTriggers.Tests.Tests.Base; using Microsoft.EntityFrameworkCore; @@ -88,6 +89,45 @@ public override void CharVariableSql() Assert.Equal('a', insertedEntity.CharValue); } + + [Fact] + public void NumberEnumSql() + { + Expression> setEnumVariableExpression = sourceEntity => new DestinationEntity + { + EnumValue = EnumValue.Value2, + }; + + var insertedEntity = ContextOptionsFactory.CheckTrigger(setEnumVariableExpression, SetupDbContext, SetupModelBuilder, new SourceEntity + { + CharValue = 'a' + }); + + Assert.Equal(EnumValue.Value2, insertedEntity.EnumValue); + } + + [Fact] + public void StringEnumSql() + { + Expression> setEnumVariableExpression = sourceEntity => new DestinationEntity + { + EnumValue = EnumValue.Value2, + }; + + Action setupModelBuilder = builder => builder.Entity() + .Property(x => x.EnumValue) + .HasColumnType("varchar(100)") + .HasConversion(); + + setupModelBuilder += SetupModelBuilder; + + var insertedEntity = ContextOptionsFactory.CheckTrigger(setEnumVariableExpression, SetupDbContext, setupModelBuilder, new SourceEntity + { + CharValue = 'a' + }); + + Assert.Equal(EnumValue.Value2, insertedEntity.EnumValue); + } public override void CharValueSql() { From 02db8e0792e8703e38d458dc4aebe5588b780c66 Mon Sep 17 00:00:00 2001 From: Ilya Belyanskiy Date: Fri, 16 Sep 2022 17:57:44 +0300 Subject: [PATCH 3/5] upd ef core version --- .../Laraue.EfCoreTriggers.Common.csproj | 6 +++--- .../Laraue.EfCoreTriggers.MySqlTests.csproj | 4 ++-- .../Laraue.EfCoreTriggers.PostgreSqlTests.csproj | 4 ++-- .../Laraue.EfCoreTriggers.SqlLiteTests.csproj | 6 +++--- .../Laraue.EfCoreTriggers.SqlServerTests.csproj | 6 +++--- .../Laraue.EfCoreTriggers.TestMigration.csproj | 4 ++-- 6 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj b/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj index afe60bf7..4efcc5f9 100644 --- a/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj +++ b/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj @@ -22,11 +22,11 @@ - - + + all - + diff --git a/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj b/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj index 8d3d5769..01277193 100644 --- a/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj +++ b/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj @@ -31,10 +31,10 @@ - + all - + all diff --git a/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj b/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj index 81185ebc..7c890994 100644 --- a/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj +++ b/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj @@ -31,10 +31,10 @@ - + all - + all diff --git a/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj b/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj index d15dd363..90dfe44a 100644 --- a/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj +++ b/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj @@ -31,11 +31,11 @@ - - + + all - + all diff --git a/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj b/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj index 35ca6bb4..356e32b4 100644 --- a/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj +++ b/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj @@ -31,11 +31,11 @@ - - + + all - + all diff --git a/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj b/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj index dc53baeb..7c02c377 100644 --- a/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj +++ b/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj @@ -22,10 +22,10 @@ - + all - + all From 072f480f8a7d69889fb6892897a65f17ec46918e Mon Sep 17 00:00:00 2001 From: Ilya Belyanskiy Date: Sat, 29 Oct 2022 12:40:49 +0300 Subject: [PATCH 4/5] ISS-44: string enums support added --- .../Enumerable/Count/CountVisitor.cs | 4 +-- .../DbContextOptionsBuilderExtensions.cs | 1 - .../Services/IDbSchemaRetriever.cs | 8 ++--- .../Services/Impl/EfCoreDbSchemaRetriever.cs | 35 +++++++++---------- .../BinaryExpressionVisitor.cs | 8 +++-- .../LambdaExpressionVisitor.cs | 4 +-- .../UnaryExpressionVisitor.cs | 29 +++++++++++++-- .../Services/Impl/SqlGenerator.cs | 5 ++- .../SqliteNewExpressionVisitor.cs | 1 - .../SqlServerTriggerVisitor.cs | 12 ++++--- .../SqlServerUnaryExpressionVisitor.cs | 9 ++--- .../TestDbContext.cs | 1 - .../Infrastructure/StandardMathRound.cs | 2 -- .../Tests/ConditionGeneratingTests.cs | 1 - .../NativeMemberAssignmentFunctionsTests.cs | 20 +++++++---- 15 files changed, 81 insertions(+), 59 deletions(-) diff --git a/src/Laraue.EfCoreTriggers.Common/Converters/MethodCall/Enumerable/Count/CountVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Converters/MethodCall/Enumerable/Count/CountVisitor.cs index e893e4e7..759eeb20 100644 --- a/src/Laraue.EfCoreTriggers.Common/Converters/MethodCall/Enumerable/Count/CountVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Converters/MethodCall/Enumerable/Count/CountVisitor.cs @@ -1,6 +1,4 @@ -using System.Collections; -using System.Collections.Generic; -using System.Linq; +using System.Linq; using System.Linq.Expressions; using Laraue.EfCoreTriggers.Common.Services; using Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; diff --git a/src/Laraue.EfCoreTriggers.Common/Extensions/DbContextOptionsBuilderExtensions.cs b/src/Laraue.EfCoreTriggers.Common/Extensions/DbContextOptionsBuilderExtensions.cs index a5d73ca2..30eef23b 100644 --- a/src/Laraue.EfCoreTriggers.Common/Extensions/DbContextOptionsBuilderExtensions.cs +++ b/src/Laraue.EfCoreTriggers.Common/Extensions/DbContextOptionsBuilderExtensions.cs @@ -1,7 +1,6 @@ using System; using Laraue.EfCoreTriggers.Common.Migrations; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Design; using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.Extensions.DependencyInjection; diff --git a/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs b/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs index cd343190..f97f9ff1 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/IDbSchemaRetriever.cs @@ -50,9 +50,9 @@ public interface IDbSchemaRetriever /// Some type can be overriden, for example Enum can be store as string in the DB. /// In these cases clr type will be returned from this function. /// - /// - /// - /// + /// Entity type. + /// Entity member. + /// Actual type if it annotation was found. /// - bool TryGetActualClrType(Type type, MemberInfo memberInfo, out Type clrType); + Type GetActualClrType(Type type, MemberInfo memberInfo); } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs index e7db2330..8afa5ece 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/EfCoreDbSchemaRetriever.cs @@ -3,7 +3,6 @@ using System.Linq; using System.Reflection; using Microsoft.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata; namespace Laraue.EfCoreTriggers.Common.Services.Impl; @@ -14,17 +13,17 @@ public class EfCoreDbSchemaRetriever : IDbSchemaRetriever /// /// Cached column names for entity properties. /// - private static readonly Dictionary ColumnNamesCache = new(); + private readonly Dictionary _columnNamesCache = new(); /// /// Cached table names for entities. /// - private static readonly Dictionary TableNamesCache = new(); + private readonly Dictionary _tableNamesCache = new(); /// /// Cached database schema names. /// - private static readonly Dictionary TableSchemasCache = new(); + private readonly Dictionary _tableSchemasCache = new(); /// /// Model used for generating SQL. From this model takes column names, table names and other meta information. @@ -43,15 +42,15 @@ public EfCoreDbSchemaRetriever(IModel model) /// public string GetColumnName(Type type, MemberInfo memberInfo) { - if (!ColumnNamesCache.ContainsKey(memberInfo)) + if (!_columnNamesCache.ContainsKey(memberInfo)) { var entityType = GetEntityType(type); var property = GetColumn(type, memberInfo); var identifier = (StoreObjectIdentifier)StoreObjectIdentifier.Create(entityType, StoreObjectType.Table); - ColumnNamesCache.Add(memberInfo, property.GetColumnName(identifier)); + _columnNamesCache.Add(memberInfo, property.GetColumnName(identifier)); } - if (!ColumnNamesCache.TryGetValue(memberInfo, out var columnName)) + if (!_columnNamesCache.TryGetValue(memberInfo, out var columnName)) { throw new InvalidOperationException($"Column name for member {memberInfo.Name} is not defined in model"); } @@ -79,13 +78,13 @@ private IEntityType GetEntityType(Type type) /// public string GetTableName(Type entity) { - if (!TableNamesCache.ContainsKey(entity)) + if (!_tableNamesCache.ContainsKey(entity)) { var entityType = Model.FindEntityType(entity); - TableNamesCache.Add(entity, entityType.GetTableName()); + _tableNamesCache.Add(entity, entityType.GetTableName()); } - if (!TableNamesCache.TryGetValue(entity, out var tableName)) + if (!_tableNamesCache.TryGetValue(entity, out var tableName)) { throw new InvalidOperationException($"Table name for entity {entity.FullName} is not defined in model."); } @@ -100,10 +99,10 @@ public string GetTableName(Type entity) /// public string GetFunctionName(Type entity, string name) { - if (!TableNamesCache.ContainsKey(entity)) + if (!_tableNamesCache.ContainsKey(entity)) { var entityType = Model.FindEntityType(entity); - TableNamesCache.Add(entity, entityType.GetTableName()); + _tableNamesCache.Add(entity, entityType.GetTableName()); } var schemaName = GetTableSchemaName(entity); @@ -120,13 +119,13 @@ public string GetFunctionName(Type entity, string name) /// protected virtual string GetTableSchemaName(Type entity) { - if (!TableSchemasCache.ContainsKey(entity)) + if (!_tableSchemasCache.ContainsKey(entity)) { var entityType = Model.FindEntityType(entity); - TableSchemasCache.Add(entity, entityType.GetSchema()); + _tableSchemasCache.Add(entity, entityType.GetSchema()); } - if (!TableSchemasCache.TryGetValue(entity, out var schemaName)) + if (!_tableSchemasCache.TryGetValue(entity, out var schemaName)) { throw new InvalidOperationException($"Schema for entity {entity.FullName} is not defined in model."); } @@ -167,12 +166,10 @@ public KeyInfo[] GetForeignKeyMembers(Type type, Type type2) return keys; } - public bool TryGetActualClrType(Type type, MemberInfo memberInfo, out Type clrType) + public Type GetActualClrType(Type type, MemberInfo memberInfo) { var columnType = GetColumn(type, memberInfo); - clrType = columnType.FindAnnotation("ProviderClrType")?.Value as Type; - - return clrType is not null; + return columnType.FindAnnotation("ProviderClrType")?.Value as Type ?? columnType.ClrType; } } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs index c2c50090..6121d66a 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/BinaryExpressionVisitor.cs @@ -35,9 +35,11 @@ public override SqlBuilder Visit( && expression.Right is ConstantExpression constantExpression) { // Convert(enumValue, Int32) == 1 when enum is stores as string -> enumValue == Enum.Value - if (memberExpression.Type.IsEnum && _schemaRetriever.TryGetActualClrType( - memberExpression.Member.DeclaringType, memberExpression.Member, out var clrType) - && clrType == typeof(string)) + var clrType = _schemaRetriever.GetActualClrType( + memberExpression.Member.DeclaringType, + memberExpression.Member); + + if (memberExpression.Type.IsEnum && clrType == typeof(string)) { var valueName = Enum.GetValues(memberExpression.Type) .Cast() diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/LambdaExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/LambdaExpressionVisitor.cs index 810e7cd0..1bf2e990 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/LambdaExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/LambdaExpressionVisitor.cs @@ -1,6 +1,4 @@ -using System; -using System.Linq.Expressions; -using Laraue.EfCoreTriggers.Common.Services.Impl.SetExpressionVisitors; +using System.Linq.Expressions; using Laraue.EfCoreTriggers.Common.SqlGeneration; using Laraue.EfCoreTriggers.Common.TriggerBuilders; diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/UnaryExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/UnaryExpressionVisitor.cs index ce656b9f..f03e4e9b 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/UnaryExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/ExpressionVisitors/UnaryExpressionVisitor.cs @@ -10,12 +10,17 @@ public class UnaryExpressionVisitor : BaseExpressionVisitor { private readonly IExpressionVisitorFactory _factory; private readonly ISqlGenerator _generator; + private readonly IDbSchemaRetriever _dbSchemaRetriever; /// - public UnaryExpressionVisitor(IExpressionVisitorFactory factory, ISqlGenerator generator) + public UnaryExpressionVisitor( + IExpressionVisitorFactory factory, + ISqlGenerator generator, + IDbSchemaRetriever dbSchemaRetriever) { _factory = factory; _generator = generator; + _dbSchemaRetriever = dbSchemaRetriever; } /// @@ -58,8 +63,14 @@ public override SqlBuilder Visit(UnaryExpression expression, ArgumentTypes argum /// protected virtual bool IsNeedConversion(UnaryExpression unaryExpression) { - var clrType1 = unaryExpression.Operand.Type; - var clrType2 = unaryExpression.Type; + // Do not execute conversion Type? -> Type, it is actual for CLR only + if (Nullable.GetUnderlyingType(unaryExpression.Type) != null) + { + return false; + } + + var clrType1 = GetActualClrType(unaryExpression.Operand); + var clrType2 = GetActualClrType(unaryExpression); if (clrType1 == typeof(object) || clrType2 == typeof(object)) { return false; @@ -84,4 +95,16 @@ protected virtual string GetConvertExpressionSql(UnaryExpression unaryExpression ? $"CAST({member} AS {sqlType})" : throw new NotSupportedException($"Converting of type {unaryExpression.Type} is not supported"); } + + private Type GetActualClrType(Expression expression) + { + if (expression is not MemberExpression {Expression: ParameterExpression parameterExpression} memberExpression) + { + return EfCoreTriggersHelper.GetNotNullableType(expression.Type); + } + + return _dbSchemaRetriever.GetActualClrType( + EfCoreTriggersHelper.GetNotNullableType(parameterExpression.Type), + memberExpression.Member); + } } \ No newline at end of file diff --git a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs index 25feddab..d722c94f 100644 --- a/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs +++ b/src/Laraue.EfCoreTriggers.Common/Services/Impl/SqlGenerator.cs @@ -88,10 +88,9 @@ public string GetSql(char source) public string GetSql(Enum source) { - _adapter.TryGetActualClrType( + var clrType = _adapter.GetActualClrType( _visitingInfo.CurrentMember.DeclaringType, - _visitingInfo.CurrentMember, - out var clrType); + _visitingInfo.CurrentMember); return clrType == typeof(string) ? GetSql(source.ToString()) diff --git a/src/Laraue.EfCoreTriggers.SqlLite/SqliteNewExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.SqlLite/SqliteNewExpressionVisitor.cs index 698e82d2..509959be 100644 --- a/src/Laraue.EfCoreTriggers.SqlLite/SqliteNewExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.SqlLite/SqliteNewExpressionVisitor.cs @@ -1,5 +1,4 @@ using Laraue.EfCoreTriggers.Common.Services; -using Laraue.EfCoreTriggers.Common.Services.Impl; using Laraue.EfCoreTriggers.Common.Services.Impl.ExpressionVisitors; using Laraue.EfCoreTriggers.Common.SqlGeneration; diff --git a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs index 3b5e14d5..5e61d5a0 100644 --- a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs +++ b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerTriggerVisitor.cs @@ -111,7 +111,7 @@ private string DeclareVariablesSql(Type triggerEntityType, VisitedMembers visite x => x.Key, x => x.Value.WhereDeclaringType(triggerEntityType)) .Where(x => x.Value.Any()) - .SelectMany(x => DeclareVariablesSql(x.Key, x.Value)) + .SelectMany(x => DeclareVariablesSql(triggerEntityType, x.Key, x.Value)) .ToArray(); if (variablesSql.Any()) @@ -123,16 +123,18 @@ private string DeclareVariablesSql(Type triggerEntityType, VisitedMembers visite return sqlBuilder; } - private IEnumerable DeclareVariablesSql(ArgumentType argumentType, IEnumerable visitedMembers) + private IEnumerable DeclareVariablesSql(Type triggerType, ArgumentType argumentType, IEnumerable visitedMembers) { return visitedMembers - .Select(member => GetDeclareVariableNameSql(argumentType, member)) + .Select(member => GetDeclareVariableNameSql(triggerType, argumentType, member)) .ToArray(); } - private string GetDeclareVariableNameSql(ArgumentType argumentType, MemberInfo member) + private string GetDeclareVariableNameSql(Type triggerType, ArgumentType argumentType, MemberInfo member) { - var sqlType = _sqlGenerator.GetSqlType(((PropertyInfo) member).PropertyType); + var clrType = _dbSchemaRetriever.GetActualClrType(triggerType, member); + + var sqlType = _sqlGenerator.GetSqlType(clrType); return $"{GetVariableNameSql(argumentType, member)} {sqlType}"; } diff --git a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerUnaryExpressionVisitor.cs b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerUnaryExpressionVisitor.cs index dcb7d797..41357e21 100644 --- a/src/Laraue.EfCoreTriggers.SqlServer/SqlServerUnaryExpressionVisitor.cs +++ b/src/Laraue.EfCoreTriggers.SqlServer/SqlServerUnaryExpressionVisitor.cs @@ -11,12 +11,13 @@ namespace Laraue.EfCoreTriggers.SqlServer; public class SqlServerUnaryExpressionVisitor : UnaryExpressionVisitor { public SqlServerUnaryExpressionVisitor( - IExpressionVisitorFactory factory, - ISqlGenerator sqlGenerator) - : base(factory, sqlGenerator) + IExpressionVisitorFactory factory, + ISqlGenerator generator, + IDbSchemaRetriever dbSchemaRetriever) + : base(factory, generator, dbSchemaRetriever) { } - + public override SqlBuilder Visit(UnaryExpression expression, ArgumentTypes argumentTypes, VisitedMembers visitedMembers) { var unarySql = base.Visit(expression, argumentTypes, visitedMembers); diff --git a/tests/Laraue.EfCoreTriggers.TestMigration/TestDbContext.cs b/tests/Laraue.EfCoreTriggers.TestMigration/TestDbContext.cs index 41684f08..d65deedf 100644 --- a/tests/Laraue.EfCoreTriggers.TestMigration/TestDbContext.cs +++ b/tests/Laraue.EfCoreTriggers.TestMigration/TestDbContext.cs @@ -1,6 +1,5 @@ using System; using Laraue.EfCoreTriggers.Common.Extensions; -using Laraue.EfCoreTriggers.MySql; using Laraue.EfCoreTriggers.MySql.Extensions; using Microsoft.EntityFrameworkCore; diff --git a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/StandardMathRound.cs b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/StandardMathRound.cs index 125b9c19..3c761ee6 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/StandardMathRound.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Infrastructure/StandardMathRound.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Laraue.EfCoreTriggers.Tests.Infrastructure { diff --git a/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs b/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs index d64e0e35..b09d114e 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Tests/ConditionGeneratingTests.cs @@ -2,7 +2,6 @@ using Laraue.EfCoreTriggers.Common.Services.Impl.TriggerVisitors; using Laraue.EfCoreTriggers.Common.SqlGeneration; using Laraue.EfCoreTriggers.Common.TriggerBuilders; -using Laraue.EfCoreTriggers.Common.TriggerBuilders.Base; using Laraue.EfCoreTriggers.Common.TriggerBuilders.OnInsert; using Laraue.EfCoreTriggers.Common.TriggerBuilders.OnUpdate; using Laraue.EfCoreTriggers.MySql.Extensions; diff --git a/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs b/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs index 6125b900..85fed239 100644 --- a/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs +++ b/tests/Laraue.EfCoreTriggers.Tests/Tests/Native/NativeMemberAssignmentFunctionsTests.cs @@ -111,19 +111,27 @@ public void StringEnumSql() { Expression> setEnumVariableExpression = sourceEntity => new DestinationEntity { - EnumValue = EnumValue.Value2, + EnumValue = sourceEntity.EnumValue, }; - Action setupModelBuilder = builder => builder.Entity() - .Property(x => x.EnumValue) - .HasColumnType("varchar(100)") - .HasConversion(); + Action setupModelBuilder = builder => + { + builder.Entity() + .Property(x => x.EnumValue) + .HasColumnType("varchar(100)") + .HasConversion(); + + builder.Entity() + .Property(x => x.EnumValue) + .HasColumnType("varchar(100)") + .HasConversion(); + }; setupModelBuilder += SetupModelBuilder; var insertedEntity = ContextOptionsFactory.CheckTrigger(setEnumVariableExpression, SetupDbContext, setupModelBuilder, new SourceEntity { - CharValue = 'a' + EnumValue = EnumValue.Value2 }); Assert.Equal(EnumValue.Value2, insertedEntity.EnumValue); From 48ab9f23cc610f98e87da18577e43d4e1ba32c36 Mon Sep 17 00:00:00 2001 From: Ilya Belyanskiy Date: Sat, 29 Oct 2022 12:53:06 +0300 Subject: [PATCH 5/5] update library versions --- .../Laraue.EfCoreTriggers.Common.csproj | 16 ++++++---------- .../Laraue.EfCoreTriggers.MySqlTests.csproj | 8 ++++---- .../Laraue.EfCoreTriggers.PostgreSqlTests.csproj | 8 ++++---- .../Laraue.EfCoreTriggers.SqlLiteTests.csproj | 12 ++++++------ .../Laraue.EfCoreTriggers.SqlServerTests.csproj | 12 ++++++------ .../Laraue.EfCoreTriggers.TestMigration.csproj | 8 ++++---- 6 files changed, 30 insertions(+), 34 deletions(-) diff --git a/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj b/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj index 4efcc5f9..4d2a6e93 100644 --- a/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj +++ b/src/Laraue.EfCoreTriggers.Common/Laraue.EfCoreTriggers.Common.csproj @@ -14,23 +14,19 @@ - - + + all - + - - + + all - - - - - + \ No newline at end of file diff --git a/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj b/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj index 01277193..38905f68 100644 --- a/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj +++ b/tests/Laraue.EfCoreTriggers.MySqlTests/Laraue.EfCoreTriggers.MySqlTests.csproj @@ -21,20 +21,20 @@ - + all - + all - + all - + all diff --git a/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj b/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj index 7c890994..f12e92a4 100644 --- a/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj +++ b/tests/Laraue.EfCoreTriggers.PostgreSqlTests/Laraue.EfCoreTriggers.PostgreSqlTests.csproj @@ -21,20 +21,20 @@ - + all - + all - + all - + all diff --git a/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj b/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj index 90dfe44a..432b0371 100644 --- a/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj +++ b/tests/Laraue.EfCoreTriggers.SqlLiteTests/Laraue.EfCoreTriggers.SqlLiteTests.csproj @@ -20,22 +20,22 @@ - + - + all - + all - - + + all - + all diff --git a/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj b/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj index 356e32b4..953c2f63 100644 --- a/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj +++ b/tests/Laraue.EfCoreTriggers.SqlServerTests/Laraue.EfCoreTriggers.SqlServerTests.csproj @@ -20,22 +20,22 @@ - + - + all - + all - - + + all - + all diff --git a/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj b/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj index 7c02c377..a4b004a3 100644 --- a/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj +++ b/tests/Laraue.EfCoreTriggers.TestMigration/Laraue.EfCoreTriggers.TestMigration.csproj @@ -12,20 +12,20 @@ - + all - + all - + all - + all