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

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

Open
CODEH4RD opened this issue Feb 4, 2025 · 3 comments

Comments

@CODEH4RD
Copy link

CODEH4RD commented Feb 4, 2025

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
@jonwagner
Copy link
Owner

I don't think there is enough information to reproduce the problem.

Ideally, submit a PR with a failing test case.

But we'll at least need a stack trace and the SQL from your stored procedure.

@CODEH4RD
Copy link
Author

CODEH4RD commented Feb 4, 2025

I don't think there is enough information to reproduce the problem.

Ideally, submit a PR with a failing test case.

But we'll at least need a stack trace and the SQL from your stored procedure.

It was hard for me :) But I create a project with local database to reproduce this issue. Here's URL for pull request:
CODEH4RD/ParentChildsIssueReproduction#1

Hope, you can help me to solve this problem! Thank you in advance!

@CODEH4RD
Copy link
Author

Does my code helps you to reproduce the problem? 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants