I have 2 tables in my db. One is country table and other is state table.
Country table has Id, name ,states[]. State table has Id, stateName, countryId.
I need to fetch a country details based on Id using .net core.
Whenever I use get operation for country using country Id - I should get the Id, name, and all state details with that country Id in an array format.
Ex. I am fetching country with Id = 1 and it has 2 states then, output should be,
Id: 1,
Name: abc,
States: [
{
Id:1,
stateName: st1,
countryId: 1
},
{
Id:2,
stateName: st2,
countryId:1
}]
Do I need to use join? Or something else. What code I need to write in DAL to get output like this? Please help!! I am new to .Net
You have two ways to fetch data.1. EF and 2.adoNet
1.EF
You can use EF
First you must add Classes (two classes) ,a class is country and other class is status ,because country has a lot of status you must use "ICollection"
Classes
This is Code in DbContext(must add classes)
This is Code to insert Data(Country,Status) that Countryid after savechange It takes a value
This is Code to fetch Data(Country,Status) that use Include for fetch states with Country
2.with Ado.net