public class BaseModel
{
[Key]
private int _Id;
public int Id {
get { return _Id; }
set { _Id = value; }
}
}
public class SupplierModel : BaseModel
{
[ForeginKey("CountryCode")] // This should map to say "se" or "no" or whatever in the CountryModel table
public virtual CountryModel Country;
}
public class CountryModel : BaseModel
{
private string _CountryCode;
[Key] // This should be another key in the table to get the actual country.
public string CountryCode {
get { return _CountryCode; }
set { _CountryCode = value; }
}
private string _CountryName;
public string CountryName {
get { return _CountryName; }
set { _CountryName = value; }
}
}
现在,我希望SupplierModel链接到CountryModel(按ID可以正常工作),但我希望它成为国家/地区代码,而不是实体之间的ID。
因此,访问CountryModel.Country应该映射到CountryModel表并拉出与国家模型匹配的表。
希望我没有完全为您弄乱,当我不完全了解实体框架和数据库关系时很难解释..尝试学习=)