Skip to content

Commit

Permalink
Add 1:1 relationship tests and implement suggestions from PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Vekz committed May 17, 2022
1 parent da61719 commit d7ef011
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}

/// <summary>
Expand All @@ -79,21 +74,13 @@ public void ProcessForeignKeyPrincipalEndChanged(IConventionForeignKeyBuilder re
}

var navigation = relationshipBuilder.Metadata.DependentToPrincipal;
if (navigation == null)
{
return;
}

var navAttribute = navigation.PropertyInfo?.GetCustomAttribute<DeleteBehaviorAttribute>();
var navAttribute = navigation?.PropertyInfo?.GetCustomAttribute<DeleteBehaviorAttribute>();
if (navAttribute == null)
{
return;
}

if (relationshipBuilder.Metadata.GetDeleteBehaviorConfigurationSource() != ConfigurationSource.Explicit)
{
relationshipBuilder.Metadata.SetDeleteBehavior(navAttribute.Behavior);
}
relationshipBuilder.OnDelete(navAttribute.Behavior, fromDataAnnotation: true);
}

/// <summary>
Expand All @@ -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<DeleteBehaviorAttribute>();
if (navAttribute == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,32 @@ public void Throw_InvalidOperationException_if_attribute_was_set_on_principal_na
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<Post_On_Principal>()
.Property(e => e.Blog_On_PrincipalId);

Assert.Equal(
CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty,
Assert.Throws<InvalidOperationException>(() =>
modelBuilder.Entity<Post_On_Principal>()
.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<Post_On_Principal_OneToOne>()
.Property(e => e.Blog_On_PrincipalId);

Assert.Equal(
CoreStrings.DeleteBehaviorAttributeOnPrincipalProperty,
Assert.Throws<InvalidOperationException>(() =>
modelBuilder.FinalizeModel()).Message
);
}


#region DeleteBehaviorAttribute not set
private class Blog
{
Expand Down Expand Up @@ -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();
}

0 comments on commit d7ef011

Please sign in to comment.