Skip to content

Upgrade process fails on nested fields when secondary Key attribute is removed without removing the field #343

Open

Description

@BrendenAZ

Problem:
The upgrade process fails when removing a Key attribute without removing the field itself. The exception is thrown in UpgradeHintsProcessor while processing nested fields.

Error:
The inner loop for nested fields is returning an error on Single because the SecondaryId field existed in the fieldMapping, but not in the nested fields of the self referencing MergedTo field where the SecondaryId was already removed.

Proposed solution:
Before checking if we need to map recursively, I think we should perform a null check to see if the nested field still exists by changing Single to SingleOrDefault.

var newNestedField = newField.Fields
            .Single(field => field.OriginalName.Equals(newNestedFieldOrigin.Name, StringComparison.Ordinal));

Before state: Model with two key fields and a self referencing field that tracks both keys.

  [HierarchyRoot(InheritanceSchema.ConcreteTable)]
  public sealed class MultiKeyEntity: Entity
  {
    [Field, Key(0)]
    public long Id { get; private set; }

    [Field, Key(1)]
    public long SecondaryId { get; private set; }

    [Field(Nullable = true)]
    public MultiKeyEntity? MergedTo { get; set; }
  }

After state: Model with one key field (second field kept, but key attribute removed) and a self referencing field that tracks remaining key.

  [HierarchyRoot(InheritanceSchema.ConcreteTable)]
  public sealed class MultiKeyEntity: Entity
  {
    [Field, Key]
    public long Id { get; private set; }

    [Field]
    public long SecondaryId { get; private set; }

    [Field(Nullable = true)]
    public MultiKeyEntity? MergedTo { get; set; }
  }

Code block:

private void MapNestedFields(StoredFieldInfo oldField, StoredFieldInfo newField)
    {
      var oldNestedFields = oldField.Fields;
      if (oldNestedFields.Length == 0) {
        return;
      }

      var oldValueType = extractedModel.Types
        .Single(type => type.UnderlyingType.Equals(oldField.ValueType, StringComparison.Ordinal));
      foreach (var oldNestedField in oldNestedFields) {
        var oldNestedFieldOriginalName = oldNestedField.OriginalName;
        var oldNestedFieldOrigin = oldValueType.AllFields
          .Single(field => field.Name.Equals(oldNestedField.OriginalName, StringComparison.Ordinal));

        if (fieldMapping.TryGetValue(oldNestedFieldOrigin, out var newNestedFieldOrigin)) {
          var newNestedField = newField.Fields
            .Single(field => field.OriginalName.Equals(newNestedFieldOrigin.Name, StringComparison.Ordinal));
          MapFieldRecursively(oldNestedField, newNestedField);
        }
      }
    }

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions