这是我猜想的每个人的噩梦。那么......至少这是我设计其中一个应用程序时的噩梦。
根据你的情景,你可能会把国家,城市,地区作为不同的实体。一切都是用这种方法找到的,直到你希望用户选择国家,地区或城市。看起来您需要具有空字段,这并不是最佳实践,因为您将不得不完全依赖应用程序逻辑来维护数据完整性。这种做法的
例子是:
public class Country
{
public string Code { get; set; } //country ID would not make sense in this approach
public string Name { get; set; }
}
public class Region
{
public string Code { get; set; }
public string Name { get; set; }
public string CountryCode { get; set; } //1 region is assigned to only 1 country
}
public class City
{
public string Code { get; set; }
public string Name { get; set; }
public string RegionCode { get; set; } //1 city is assigned to only 1 region
}
它看起来不错,简单易懂但想想,你捕捉什么被选择的表。如果你只关心城市(依赖列表中的最后一项),那一切都很清楚。
public class UserSelectionWithCityOnly
{
public string CityCode { get; set; }
}
很容易和直截了当?看起来是这样。 考虑场景,你可以选择任何一个国家,城市或区域....它得到的真的很乱:
public class UserSelectionWithEitherSelected
{
public string? CityCode { get; set; }
public string? RegionCode { get; set; }
public string? CountryCode { get; set; }
}
嗯......你总是可以检查是否CityCode.HasValue,但从DB点将是一个可空场,可在其中加脏数据(如果你不是迂腐大约有干净整洁的DB应该是罚款)
所以他们的方式我解决,这是通过创建与父项ID一个阶层表:
public class MySolutionForDestinations
{
public int DestinationId { get; set; } //primary key
public int ParentDestinationId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public DestinationLevel Level { get; set; }
}
public enum DestinationLevel
{
Country = 0,
Region = 1,
City = 2
}
它可能不是最优雅的解决方案,但它工作得很好。在这种方法中,你只关心DestinationId,它可以是一个国家ID,地区ID或城市ID,所以你肯定会避免有脏数据,并可以实现1对1映射。
希望这将是有用的
我会做'国家VideoLocation;'位置重复的就是你已经离开。 – Reniuz 2012-03-13 11:12:47
如果我做国家VideoLocation如何按城市查询例如? – CSharpLearning 2012-03-13 11:15:18
'select * from Video where Video.VideoLocation.CountryId = selectedCity.FK_CountryId' – Reniuz 2012-03-13 11:19:32