Skip to content

Construction of a parent/child relationship using a single result set exception #524

Description

@HaMMeRSMeeT

Describe the bug

I have a ASP.NET Core API. When I'm trying to get parent/child results from single dataset from SQL Server stored procedure, I got System.NullReferenceException: "Object reference not set to an instance of an object." error.

Steps to reproduce

  1. Dataset
Id	ParentId	Index	Name
1	NULL	1		[{"Key":"en","Value":"Priority 1"}]
2	1		1		[{"Key":"en","Value":"Priority 1 Subpriority 1"}]
3	1		2		[{"Key":"en","Value":"Priority 1 Subpriority 2"}]
4	1		3		[{"Key":"en","Value":"Priority 1 Subpriority 3"}]
7	1		4		[{"Key":"en","Value":"Priority 1 Subpriority 4"}]
8	1		5		[{"Key":"en","Value":"Priority 1 Subpriority 5"}]
9	NULL	2		[{"Key":"en","Value":"Priority 2"}]
11	9		1		[{"Key":"en","Value":"Priority 2 Subpriority 1"}]
12	9		2		[{"Key":"en","Value":"Priority 2 Subpriority 2"}]
  1. Model
    public class PriorityModel
    {
        [RecordId]
        public int Id { get; set; }

        [ParentRecordId]
        public int? ParentId { get; set; }

        public int IndexNumber { get; set; }

        [Column(SerializationMode = SerializationMode.Json)]
        public required IEnumerable<LocalizedNameModel> Name { get; set; }

        [ChildRecords]
        public IList<PriorityModel>? Children { get; set; }
    }

    public class LocalizedNameModel
    {
        public required string Key { get; set; }
        public required string Value { get; set; }
    }
  1. Controller
        public IActionResult GetPriorities(bool showOnlyTopLevel, bool showAllLanguages, string language = "en")
        {
            using var cnn = new SqlConnection(connectionString);

            var result = cnn.Query("ref.GetPriorities", new { showOnlyTopLevel, showAllLanguages, language },
                Query.Returns(Together<PriorityModel, PriorityModel>.Records));
            return Ok(result);
        }

Expected behavior

I expect following JSON on exit:

[
    {
        "Id": 1,
        "ParentId": null,
        "Index": 1,
        "Name": {
            "Key": "en",
            "Value": "Priority 1"
        },
        "Children": [
            {
                "Id": 2,
                "ParentId": 1,
                "Index": 1,
                "Name": {
                    "Key": "en",
                    "Value": "Priority 1 Subpriority 1"
                },
                "Children": null
            },
            {
                "Id": 3,
                "ParentId": 1,
                "Index": 2,
                "Name": {
                    "Key": "en",
                    "Value": "Priority 1 Subpriority 2"
                },
                "Children": null
            }
        ]
    }
]
  • Dotnet version: netcore8.0.12
  • Database: SQL Server 2022
  • Library version: 8.0.3

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

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions