Skip to content

Commit

Permalink
Don't warn for composite keys with at least one non-default property.
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Sep 12, 2022
1 parent 01c6294 commit 6937078
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,7 @@ private static void ValidateSproc(IStoredProcedure sproc, string mappingStrategy
entityType.DisplayName(),
property.GetBeforeSaveBehavior()));
}

break;

case StoreObjectType.UpdateStoredProcedure:
Expand Down Expand Up @@ -708,15 +709,27 @@ protected virtual void ValidateDefaultValuesOnKeys(
{
foreach (var key in entityType.GetDeclaredKeys())
{
foreach (var property in key.Properties.Where(p => !p.IsForeignKey()))
IProperty? propertyWithDefault = null;
foreach (var property in key.Properties)
{
var defaultValue = (IConventionAnnotation?)property.FindAnnotation(RelationalAnnotationNames.DefaultValue);
if (defaultValue?.Value != null
if (!property.IsForeignKey()
&& defaultValue?.Value != null
&& defaultValue.GetConfigurationSource().Overrides(ConfigurationSource.DataAnnotation))
{
logger.ModelValidationKeyDefaultValueWarning(property);
propertyWithDefault ??= property;
}
else
{
propertyWithDefault = null;
break;
}
}

if (propertyWithDefault != null)
{
logger.ModelValidationKeyDefaultValueWarning(propertyWithDefault);
}
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions test/EFCore.Specification.Tests/DataAnnotationTestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,12 @@ protected class DependantA
public PrincipalA Principal { get; set; }
}

protected class PrincipalB
{
public int Id1 { get; set; }
public int Id2 { get; set; }
}

[ConditionalFact]
public virtual IModel Key_and_column_work_together()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,43 @@ public virtual void Default_for_key_which_is_also_an_fk_column_does_not_throw()
Validate(modelBuilder);
}

[ConditionalFact]
public virtual void Default_for_part_of_composite_key_does_not_throw()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<PrincipalB>(
b =>
{
b.HasKey(e => new { e.Id1, e.Id2 });
b.Property(e => e.Id1).HasDefaultValue(77);
});

Validate(modelBuilder);
}

[ConditionalFact]
public virtual void Default_for_all_parts_of_composite_key_throws()
{
var modelBuilder = CreateModelBuilder();

modelBuilder.Entity<PrincipalB>(
b =>
{
b.HasKey(e => new { e.Id1, e.Id2 });
b.Property(e => e.Id1).HasDefaultValue(77);
b.Property(e => e.Id2).HasDefaultValue(78);
});

Assert.Equal(
CoreStrings.WarningAsErrorTemplate(
RelationalEventId.ModelValidationKeyDefaultValueWarning,
RelationalResources.LogKeyHasDefaultValue(new TestLogger<SqlServerLoggingDefinitions>())
.GenerateMessage(nameof(PrincipalB.Id1), nameof(PrincipalB)),
"RelationalEventId.ModelValidationKeyDefaultValueWarning"),
Assert.Throws<InvalidOperationException>(() => Validate(modelBuilder)).Message);
}

public override IModel Non_public_annotations_are_enabled()
{
var model = base.Non_public_annotations_are_enabled();
Expand Down

0 comments on commit 6937078

Please sign in to comment.