From d7ef01192d41e5e45ffdfa837f318b2e186c9a85 Mon Sep 17 00:00:00 2001 From: Artur Porowski Date: Tue, 17 May 2022 22:56:15 +0200 Subject: [PATCH] Add 1:1 relationship tests and implement suggestions from PR --- .../DeleteBehaviorAttributeConvention.cs | 34 ++++----------- .../DeleteBehaviorAttributeConventionTest.cs | 41 ++++++++++++++++++- 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs b/src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs index 1ca8ce25c83..c721e6be012 100644 --- a/src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs +++ b/src/EFCore/Metadata/Conventions/DeleteBehaviorAttributeConvention.cs @@ -1,6 +1,9 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. +using Microsoft.EntityFrameworkCore.Diagnostics; +using Microsoft.EntityFrameworkCore.Metadata.Builders; +using Microsoft.EntityFrameworkCore.Metadata.Conventions.Infrastructure; using Microsoft.EntityFrameworkCore.Metadata.Internal; namespace Microsoft.EntityFrameworkCore.Metadata.Conventions; @@ -49,21 +52,13 @@ public void ProcessNavigationAdded(IConventionNavigationBuilder navigationBuilde return; } - if (!navigationBuilder.Metadata.IsOnDependent) - { - throw new InvalidOperationException(CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty); - } - var foreignKey = navigationBuilder.Metadata.ForeignKey; - if (foreignKey.IsUnique) + if (!navigationBuilder.Metadata.IsOnDependent && foreignKey.IsUnique) { return; } - - if (foreignKey.GetDeleteBehaviorConfigurationSource() != ConfigurationSource.Explicit) - { - foreignKey.SetDeleteBehavior(navAttribute.Behavior); - } + + foreignKey.Builder.OnDelete(navAttribute.Behavior, fromDataAnnotation: true); } /// @@ -79,21 +74,13 @@ public void ProcessForeignKeyPrincipalEndChanged(IConventionForeignKeyBuilder re } var navigation = relationshipBuilder.Metadata.DependentToPrincipal; - if (navigation == null) - { - return; - } - - var navAttribute = navigation.PropertyInfo?.GetCustomAttribute(); + var navAttribute = navigation?.PropertyInfo?.GetCustomAttribute(); if (navAttribute == null) { return; } - if (relationshipBuilder.Metadata.GetDeleteBehaviorConfigurationSource() != ConfigurationSource.Explicit) - { - relationshipBuilder.Metadata.SetDeleteBehavior(navAttribute.Behavior); - } + relationshipBuilder.OnDelete(navAttribute.Behavior, fromDataAnnotation: true); } /// @@ -107,11 +94,6 @@ public void ProcessModelFinalizing(IConventionModelBuilder modelBuilder, IConven { foreach (var navigation in entityType.GetNavigations()) { - if (!navigation.ForeignKey.IsUnique) - { - return; - } - var navAttribute = navigation.PropertyInfo?.GetCustomAttribute(); if (navAttribute == null) { diff --git a/test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs b/test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs index 02c88739741..edcaf6b3583 100644 --- a/test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs +++ b/test/EFCore.Tests/Metadata/Conventions/DeleteBehaviorAttributeConventionTest.cs @@ -116,15 +116,32 @@ public void Throw_InvalidOperationException_if_attribute_was_set_on_principal_na { var modelBuilder = CreateModelBuilder(); + modelBuilder.Entity() + .Property(e => e.Blog_On_PrincipalId); Assert.Equal( CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty, Assert.Throws(() => - modelBuilder.Entity() - .Property(e => e.Blog_On_PrincipalId)).Message + modelBuilder.FinalizeModel()).Message ); } + [ConditionalFact] + public void Throw_InvalidOperationException_if_attribute_was_set_on_principal_one_to_one_relationship() + { + var modelBuilder = CreateModelBuilder(); + + modelBuilder.Entity() + .Property(e => e.Blog_On_PrincipalId); + + Assert.Equal( + CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty, + Assert.Throws(() => + modelBuilder.FinalizeModel()).Message + ); + } + + #region DeleteBehaviorAttribute not set private class Blog { @@ -272,6 +289,26 @@ private class Post_On_Principal } #endregion + #region DeleteBehaviourAttribute set on principal 1:1 relationship + private class Blog_On_Principal_OneToOne + { + public int Id { get; set; } + + [DeleteBehavior(DeleteBehavior.Restrict)] + public Post_On_Principal_OneToOne Post_On_Principal_OneToOne { get; set; } + } + + private class Post_On_Principal_OneToOne + { + public int Id { get; set; } + + public Blog_On_Principal_OneToOne Blog_On_Principal_OneToOne { get; set; } + + public int? Blog_On_PrincipalId { get; set; } + } + #endregion + + private static ModelBuilder CreateModelBuilder() => InMemoryTestHelpers.Instance.CreateConventionBuilder(); }