我有两个表在我的DB。一个是国家表,另一个是国家表。
Country表具有ID、名称、states[]。状态表具有Id、stateName、countryId。
我需要获取一个国家的详细信息的基础上使用ID。
每当我使用国家ID对国家使用get操作时-我应该以数组格式获得ID,名称和所有国家ID的详细信息。
例如,我正在获取ID = 1的国家,并且它有2个状态,那么,输出应该是,
Id: 1,
Name: abc,
States: [
{
Id:1,
stateName: st1,
countryId: 1
},
{
Id:2,
stateName: st2,
countryId:1
}]
我需要使用join吗?或者别的什么。我需要在DAL中编写什么代码才能得到这样的输出?救命啊!我是.Net的新手
1条答案
按热度按时间vwkv1x7d1#
你有两种方法来获取数据。1. EF和2.adoNet
1.EF
你可以用EF
首先你必须添加类(两个类),一个类是国家,另一个类是状态,因为国家有很多状态,你必须使用“ICollection”
课程
这是DbContext中的代码(必须添加类)
这是插入数据(国家/地区,状态)的代码,在savechchange后的Countryid
这是获取数据(国家,状态)的代码,它使用Include获取国家的状态
Ado.net