I need to create hierarchical tree from accounts for a financial accounting web-api software I created a table with self relationship but get duplicate records.
Data model:
public class Account
{
public int AccountId { get; set; }
public int AccountCode { get; set; }
[Required]
public string AccountName { get; set; }
public int AccountParentCode { get; set; }
public int? AccountParentId { get; set; }
[IgnoreDataMember]
public Account AccountParent { get; set; }
public ICollection<Account> AccountChields { get; set; }
}
return the value to api:
return _context.Accounts.ToListAsync();
My output:
[ {
"accountId": 147,
"accountCode": 1,
"accountName": "Test1",
"accountParentCode": 0,
"accountParentId": null,
"accountChields": [
{
"accountId": 149,
"accountCode": 11,
"accountName": "Test11",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": [
{
"accountId": 152,
"accountCode": 113,
"accountName": "Test113",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 153,
"accountCode": 114,
"accountName": "Test114",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 154,
"accountCode": 115,
"accountName": "Test115",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
}
]
},
{
"accountId": 150,
"accountCode": 16,
"accountName": "Test16",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": null
},
{
"accountId": 151,
"accountCode": 18,
"accountName": "Test18",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": null
}
]
},
{
"accountId": 148,
"accountCode": 2,
"accountName": "Test2",
"accountParentCode": 0,
"accountParentId": null,
"accountChields": null
},
{
"accountId": 149,
"accountCode": 11,
"accountName": "Test11",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": [
{
"accountId": 152,
"accountCode": 113,
"accountName": "Test113",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 153,
"accountCode": 114,
"accountName": "Test114",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 154,
"accountCode": 115,
"accountName": "Test115",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
}
]
},
{
"accountId": 150,
"accountCode": 16,
"accountName": "Test16",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": null
},
{
"accountId": 151,
"accountCode": 18,
"accountName": "Test18",
"accountParentCode": 1,
"accountParentId": 147,
"accountChields": null
},
{
"accountId": 152,
"accountCode": 113,
"accountName": "Test113",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 153,
"accountCode": 114,
"accountName": "Test114",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
},
{
"accountId": 154,
"accountCode": 115,
"accountName": "Test115",
"accountParentCode": 11,
"accountParentId": 149,
"accountChields": null
}
]
I'm new to using .Net technology, is there a way to accomplish this task or do I have to change the way I think about setting up the database?
Thak you;
