From 49190801f5b558cc4bbc66ce4450878138f1c41c Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Thu, 8 Sep 2022 21:32:58 -0700 Subject: [PATCH] Fix TPH sproc relational model --- .../Metadata/Internal/RelationalModel.cs | 46 ++++++++----------- .../StoredProcedureUpdateFixtureBase.cs | 2 +- .../StoredProcedureUpdateSqlServerTest.cs | 10 ++-- 3 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs index b635b1711b8..84d7f48e9eb 100644 --- a/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs +++ b/src/EFCore.Relational/Metadata/Internal/RelationalModel.cs @@ -1133,21 +1133,18 @@ private static StoredProcedureMapping CreateStoredProcedureMapping( entityType.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy, "Expected TPH for " + entityType.DisplayName()); - if (entityType.BaseType == null) + foreach (var derivedProperty in entityType.GetRootType().GetDerivedProperties()) { - foreach (var derivedProperty in entityType.GetDerivedProperties()) + if (derivedProperty.Name == parameter.PropertyName) { - if (derivedProperty.Name == parameter.PropertyName) - { - GetOrCreateStoreStoredProcedureParameter( - parameter, - derivedProperty, - position, - storeStoredProcedure, - identifier, - relationalTypeMappingSource); - break; - } + GetOrCreateStoreStoredProcedureParameter( + parameter, + derivedProperty, + position, + storeStoredProcedure, + identifier, + relationalTypeMappingSource); + break; } } @@ -1201,21 +1198,18 @@ private static StoredProcedureMapping CreateStoredProcedureMapping( entityType.GetMappingStrategy() == RelationalAnnotationNames.TphMappingStrategy, "Expected TPH for " + entityType.DisplayName()); - if (entityType.BaseType == null) + foreach (var derivedProperty in entityType.GetRootType().GetDerivedProperties()) { - foreach (var derivedProperty in entityType.GetDerivedProperties()) + if (derivedProperty.Name == resultColumn.PropertyName) { - if (derivedProperty.Name == resultColumn.PropertyName) - { - GetOrCreateStoreStoredProcedureResultColumn( - resultColumn, - derivedProperty, - position, - storeStoredProcedure, - identifier, - relationalTypeMappingSource); - break; - } + GetOrCreateStoreStoredProcedureResultColumn( + resultColumn, + derivedProperty, + position, + storeStoredProcedure, + identifier, + relationalTypeMappingSource); + break; } } diff --git a/test/EFCore.Relational.Specification.Tests/Update/StoredProcedureUpdateFixtureBase.cs b/test/EFCore.Relational.Specification.Tests/Update/StoredProcedureUpdateFixtureBase.cs index 15909628c88..ea5621967f1 100644 --- a/test/EFCore.Relational.Specification.Tests/Update/StoredProcedureUpdateFixtureBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Update/StoredProcedureUpdateFixtureBase.cs @@ -198,9 +198,9 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con .HasParameter(w => w.Id, pb => pb.IsOutput()) .HasParameter("Discriminator") .HasParameter(w => w.Name) - .HasParameter(nameof(TphChild1.Child1Property)) .HasParameter(nameof(TphChild2.Child2InputProperty)) .HasParameter(nameof(TphChild2.Child2OutputParameterProperty), o => o.IsOutput()) + .HasParameter(nameof(TphChild1.Child1Property)) .HasResultColumn(nameof(TphChild2.Child2ResultColumnProperty))); }); diff --git a/test/EFCore.SqlServer.FunctionalTests/Update/StoredProcedureUpdateSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Update/StoredProcedureUpdateSqlServerTest.cs index 89a11a91daa..6dfd279bc28 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Update/StoredProcedureUpdateSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Update/StoredProcedureUpdateSqlServerTest.cs @@ -306,12 +306,12 @@ public override async Task Tph(bool async) @"@p0=NULL (Nullable = false) (Direction = Output) (DbType = Int32) @p1='TphChild1' (Nullable = false) (Size = 4000) @p2='Child' (Size = 4000) -@p3='8' (Nullable = true) -@p4=NULL (DbType = Int32) -@p5=NULL (Direction = Output) (DbType = Int32) +@p3=NULL (DbType = Int32) +@p4=NULL (Direction = Output) (DbType = Int32) +@p5='8' (Nullable = true) SET NOCOUNT ON; -EXEC [Tph_Insert] @p0 OUTPUT, @p1, @p2, @p3, @p4, @p5 OUTPUT;"); +EXEC [Tph_Insert] @p0 OUTPUT, @p1, @p2, @p3, @p4 OUTPUT, @p5;"); } public override async Task Tpt(bool async) @@ -576,7 +576,7 @@ IF @Name IS NULL GO -CREATE PROCEDURE Tph_Insert(@Id int OUT, @Discriminator varchar(max), @Name varchar(max), @Child1Property int, @Child2InputProperty int, @Child2OutputParameterProperty int OUT) +CREATE PROCEDURE Tph_Insert(@Id int OUT, @Discriminator varchar(max), @Name varchar(max), @Child2InputProperty int, @Child2OutputParameterProperty int OUT, @Child1Property int) AS BEGIN DECLARE @Table table ([Child2OutputParameterProperty] int); INSERT INTO [Tph] ([Discriminator], [Name], [Child1Property], [Child2InputProperty]) OUTPUT [Inserted].[Child2OutputParameterProperty] INTO @Table VALUES (@Discriminator, @Name, @Child1Property, @Child2InputProperty);