Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log an error for Required on principal to dependent #21701

Merged
2 commits merged into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/EFCore/Diagnostics/CoreEventId.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
Expand Down Expand Up @@ -97,10 +98,10 @@ private enum Id
ForeignKeyAttributesOnBothNavigationsWarning,
ConflictingForeignKeyAttributesOnNavigationAndPropertyWarning,
RedundantForeignKeyWarning,
NonNullableInverted,
NonNullableInverted, // Unused
NonNullableReferenceOnBothNavigations,
NonNullableReferenceOnDependent,
RequiredAttributeInverted,
RequiredAttributeInverted, // Unused
RequiredAttributeOnCollection,
CollectionWithoutComparer,
ConflictingKeylessAndKeyAttributesWarning,
Expand Down Expand Up @@ -405,6 +406,7 @@ public static readonly EventId InvalidIncludePathError
/// This event uses the <see cref="NavigationEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
[Obsolete]
public static readonly EventId RequiredAttributeInverted = MakeModelId(Id.RequiredAttributeInverted);

/// <summary>
Expand All @@ -419,6 +421,7 @@ public static readonly EventId InvalidIncludePathError
/// This event uses the <see cref="NavigationEventData" /> payload when used with a <see cref="DiagnosticSource" />.
/// </para>
/// </summary>
[Obsolete]
public static readonly EventId NonNullableInverted = MakeModelId(Id.NonNullableInverted);

/// <summary>
Expand Down
17 changes: 9 additions & 8 deletions src/EFCore/Diagnostics/CoreLoggerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using System.Linq.Expressions;
using System.Reflection;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
Expand Down Expand Up @@ -1172,6 +1171,7 @@ private static string IncompatibleMatchingForeignKeyProperties(EventDefinitionBa
/// </summary>
/// <param name="diagnostics"> The diagnostics logger to use. </param>
/// <param name="navigation"> The navigation property. </param>
[Obsolete]
public static void RequiredAttributeInverted(
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Model> diagnostics,
[NotNull] INavigation navigation)
Expand All @@ -1180,7 +1180,7 @@ public static void RequiredAttributeInverted(

if (diagnostics.ShouldLog(definition))
{
definition.Log(diagnostics,navigation.Name, navigation.DeclaringEntityType.DisplayName());
definition.Log(diagnostics, navigation.Name, navigation.DeclaringEntityType.DisplayName());
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
Expand All @@ -1206,6 +1206,7 @@ private static string RequiredAttributeInverted(EventDefinitionBase definition,
/// </summary>
/// <param name="diagnostics"> The diagnostics logger to use. </param>
/// <param name="navigation"> The navigation property. </param>
[Obsolete]
public static void NonNullableInverted(
AndriySvyryd marked this conversation as resolved.
Show resolved Hide resolved
[NotNull] this IDiagnosticsLogger<DbLoggerCategory.Model> diagnostics,
[NotNull] INavigation navigation)
Expand All @@ -1214,7 +1215,7 @@ public static void NonNullableInverted(

if (diagnostics.ShouldLog(definition))
{
definition.Log(diagnostics,navigation.Name, navigation.DeclaringEntityType.DisplayName());
definition.Log(diagnostics, navigation.Name, navigation.DeclaringEntityType.DisplayName());
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
Expand Down Expand Up @@ -1346,7 +1347,7 @@ public static void RequiredAttributeOnDependent(
{
definition.Log(
diagnostics,
navigation.Name, navigation.DeclaringEntityType.DisplayName());
navigation.DeclaringEntityType.DisplayName(), navigation.Name);
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
Expand All @@ -1364,7 +1365,7 @@ private static string RequiredAttributeOnDependent(EventDefinitionBase definitio
{
var d = (EventDefinition<string, string>)definition;
var p = (NavigationEventData)payload;
return d.GenerateMessage(p.Navigation.Name, p.Navigation.DeclaringEntityType.DisplayName());
return d.GenerateMessage(p.Navigation.DeclaringEntityType.DisplayName(), p.Navigation.Name);
}

/// <summary>
Expand All @@ -1382,8 +1383,8 @@ public static void NonNullableReferenceOnDependent(
{
definition.Log(
diagnostics,
navigation.Name,
navigation.DeclaringEntityType.DisplayName());
navigation.DeclaringEntityType.DisplayName(),
navigation.Name);
}

if (diagnostics.NeedsEventData(definition, out var diagnosticSourceEnabled, out var simpleLogEnabled))
Expand All @@ -1401,7 +1402,7 @@ private static string NonNullableReferenceOnDependent(EventDefinitionBase defini
{
var d = (EventDefinition<string, string>)definition;
var p = (NavigationEventData)payload;
return d.GenerateMessage(p.Navigation.Name, p.Navigation.DeclaringEntityType.DisplayName());
return d.GenerateMessage(p.Navigation.DeclaringEntityType.DisplayName(), p.Navigation.Name);
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions src/EFCore/Diagnostics/LoggingDefinitions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace Microsoft.EntityFrameworkCore.Diagnostics
Expand Down Expand Up @@ -500,6 +501,7 @@ public abstract class LoggingDefinitions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
[Obsolete]
public EventDefinitionBase LogRequiredAttributeInverted;

/// <summary>
Expand All @@ -509,6 +511,7 @@ public abstract class LoggingDefinitions
/// doing so can result in application failures when updating to a new Entity Framework Core release.
/// </summary>
[EntityFrameworkInternal]
[Obsolete]
public EventDefinitionBase LogNonNullableInverted;

/// <summary>
Expand Down
3 changes: 2 additions & 1 deletion src/EFCore/Infrastructure/CoreOptionsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ private WarningsConfiguration _warningsConfiguration
.TryWithExplicit(CoreEventId.ManyServiceProvidersCreatedWarning, WarningBehavior.Throw)
.TryWithExplicit(CoreEventId.LazyLoadOnDisposedContextWarning, WarningBehavior.Throw)
.TryWithExplicit(CoreEventId.DetachedLazyLoadingWarning, WarningBehavior.Throw)
.TryWithExplicit(CoreEventId.InvalidIncludePathError, WarningBehavior.Throw);
.TryWithExplicit(CoreEventId.InvalidIncludePathError, WarningBehavior.Throw)
.TryWithExplicit(CoreEventId.RequiredAttributeOnDependent, WarningBehavior.Throw);

/// <summary>
/// Creates a new set of options with everything set to default values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public virtual ConventionSet CreateConventionSet()
var databaseGeneratedAttributeConvention = new DatabaseGeneratedAttributeConvention(Dependencies);
var requiredPropertyAttributeConvention = new RequiredPropertyAttributeConvention(Dependencies);
var nonNullableReferencePropertyConvention = new NonNullableReferencePropertyConvention(Dependencies);
var nonNullableNavigationConvention = new NonNullableNavigationConvention(Dependencies);
var maxLengthAttributeConvention = new MaxLengthAttributeConvention(Dependencies);
var stringLengthAttributeConvention = new StringLengthAttributeConvention(Dependencies);
var timestampAttributeConvention = new TimestampAttributeConvention(Dependencies);
Expand Down Expand Up @@ -169,29 +168,11 @@ public virtual ConventionSet CreateConventionSet()
conventionSet.ForeignKeyOwnershipChangedConventions.Add(relationshipDiscoveryConvention);
conventionSet.ForeignKeyOwnershipChangedConventions.Add(valueGeneratorConvention);

conventionSet.ModelInitializedConventions.Add(new DbSetFindingConvention(Dependencies));

conventionSet.ModelFinalizingConventions.Add(new ModelCleanupConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(keyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(indexAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(foreignKeyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(new ChangeTrackingStrategyConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(new ConstructorBindingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(new TypeMappingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(foreignKeyIndexConvention);
conventionSet.ModelFinalizingConventions.Add(foreignKeyPropertyDiscoveryConvention);
conventionSet.ModelFinalizingConventions.Add(servicePropertyDiscoveryConvention);
conventionSet.ModelFinalizingConventions.Add(nonNullableReferencePropertyConvention);
conventionSet.ModelFinalizingConventions.Add(nonNullableNavigationConvention);
conventionSet.ModelFinalizingConventions.Add(new QueryFilterRewritingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(inversePropertyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(backingFieldConvention);

conventionSet.ModelFinalizedConventions.Add(new ValidatingConvention(Dependencies));

var requiredNavigationAttributeConvention = new RequiredNavigationAttributeConvention(Dependencies);
var nonNullableNavigationConvention = new NonNullableNavigationConvention(Dependencies);
conventionSet.NavigationAddedConventions.Add(backingFieldConvention);
conventionSet.NavigationAddedConventions.Add(new NavigationBackingFieldAttributeConvention(Dependencies));
conventionSet.NavigationAddedConventions.Add(new RequiredNavigationAttributeConvention(Dependencies));
conventionSet.NavigationAddedConventions.Add(requiredNavigationAttributeConvention);
conventionSet.NavigationAddedConventions.Add(nonNullableNavigationConvention);
conventionSet.NavigationAddedConventions.Add(inversePropertyAttributeConvention);
conventionSet.NavigationAddedConventions.Add(foreignKeyPropertyDiscoveryConvention);
Expand All @@ -211,6 +192,8 @@ public virtual ConventionSet CreateConventionSet()
conventionSet.IndexUniquenessChangedConventions.Add(foreignKeyIndexConvention);

conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);
conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(requiredNavigationAttributeConvention);
conventionSet.ForeignKeyPrincipalEndChangedConventions.Add(nonNullableNavigationConvention);

conventionSet.PropertyNullabilityChangedConventions.Add(foreignKeyPropertyDiscoveryConvention);

Expand All @@ -224,6 +207,26 @@ public virtual ConventionSet CreateConventionSet()
conventionSet.PropertyFieldChangedConventions.Add(stringLengthAttributeConvention);
conventionSet.PropertyFieldChangedConventions.Add(timestampAttributeConvention);

conventionSet.ModelInitializedConventions.Add(new DbSetFindingConvention(Dependencies));

conventionSet.ModelFinalizingConventions.Add(new ModelCleanupConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(keyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(indexAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(foreignKeyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(new ChangeTrackingStrategyConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(new ConstructorBindingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(new TypeMappingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(foreignKeyIndexConvention);
conventionSet.ModelFinalizingConventions.Add(foreignKeyPropertyDiscoveryConvention);
conventionSet.ModelFinalizingConventions.Add(servicePropertyDiscoveryConvention);
conventionSet.ModelFinalizingConventions.Add(nonNullableReferencePropertyConvention);
conventionSet.ModelFinalizingConventions.Add(nonNullableNavigationConvention);
conventionSet.ModelFinalizingConventions.Add(new QueryFilterRewritingConvention(Dependencies));
conventionSet.ModelFinalizingConventions.Add(inversePropertyAttributeConvention);
conventionSet.ModelFinalizingConventions.Add(backingFieldConvention);

conventionSet.ModelFinalizedConventions.Add(new ValidatingConvention(Dependencies));

return conventionSet;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ public abstract class NavigationAttributeConventionBase<TAttribute> :
IEntityTypeAddedConvention,
IEntityTypeIgnoredConvention,
IEntityTypeBaseTypeChangedConvention,
IEntityTypeMemberIgnoredConvention,
INavigationAddedConvention,
IEntityTypeMemberIgnoredConvention
IForeignKeyPrincipalEndChangedConvention
where TAttribute : Attribute
{
/// <summary>
Expand Down Expand Up @@ -170,12 +171,23 @@ public virtual void ProcessNavigationAdded(
}
}

/// <summary>
/// Called after an entity type member is ignored.
/// </summary>
/// <param name="entityTypeBuilder"> The builder for the entity type. </param>
/// <param name="name"> The name of the ignored member. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
/// <inheritdoc />
public virtual void ProcessForeignKeyPrincipalEndChanged(
IConventionForeignKeyBuilder relationshipBuilder,
IConventionContext<IConventionForeignKeyBuilder> context)
{
var fk = relationshipBuilder.Metadata;
var dependentToPrincipalAttributes = fk.DependentToPrincipal == null
? null
: GetAttributes<TAttribute>(fk.DeclaringEntityType, fk.DependentToPrincipal);
var principalToDependentAttributes = fk.PrincipalToDependent == null
? null
: GetAttributes<TAttribute>(fk.PrincipalEntityType, fk.PrincipalToDependent);
ProcessForeignKeyPrincipalEndChanged(
relationshipBuilder, dependentToPrincipalAttributes, principalToDependentAttributes, context);
}

/// <inheritdoc />
public virtual void ProcessEntityTypeMemberIgnored(
IConventionEntityTypeBuilder entityTypeBuilder, string name, IConventionContext<string> context)
{
Expand Down Expand Up @@ -316,5 +328,19 @@ public virtual void ProcessEntityTypeMemberIgnored(
[NotNull] TAttribute attribute,
[NotNull] IConventionContext<string> context)
=> throw new NotImplementedException();

/// <summary>
/// Called after the principal end of a foreign key is changed.
/// </summary>
/// <param name="relationshipBuilder"> The builder for the foreign key. </param>
/// <param name="dependentToPrincipalAttributes"> The attributes on the dependent to principal navigation. </param>
/// <param name="principalToDependentAttributes"> The attributes on the principal to dependent navigation. </param>
/// <param name="context"> Additional information associated with convention execution. </param>
public virtual void ProcessForeignKeyPrincipalEndChanged(
[NotNull] IConventionForeignKeyBuilder relationshipBuilder,
[CanBeNull] IEnumerable<TAttribute> dependentToPrincipalAttributes,
[CanBeNull] IEnumerable<TAttribute> principalToDependentAttributes,
[NotNull] IConventionContext<IConventionForeignKeyBuilder> context)
=> throw new NotImplementedException();
}
}
Loading