這是我猜想的每個人的噩夢。那麼......至少這是我設計其中一個應用程序時的噩夢。
根據你的情景,你可能會把國家,城市,地區作爲不同的實體。一切都是用這種方法找到的,直到你希望用戶選擇國家,地區或城市。看起來您需要具有空字段,這並不是最佳實踐,因爲您將不得不完全依賴應用程序邏輯來維護數據完整性。這種做法的
例子是:
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