我有一个表 Country.CountryID 是另一个表 Energy 上的外键。当我尝试删除 Country 表中的记录时,我应该收到一条显示外键关系的消息。该消息还应该显示子表的名称。我使用过 jtable。
编辑:模型是:-
public class Country
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CountryID { get; set; }
[Required(ErrorMessage = "The Country Name is required")]
public string CountryName { get; set; }
}
public class Energy
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int EnergyID { get; set; }
[Required(ErrorMessage = "Country is required.")]
public int? CountryID { get; set; }
[ForeignKey("CountryID")]
public virtual Country Countries { get; set; }
}
在控制器内部,我这样写:-
SqlException sqlException = ex.InnerException != null ? ex.InnerException.InnerException as SqlException : null;
if (sqlException != null) return Json(new { Result = "ERROR", Message = "Sorry! unable to delete." });
else return Json(new { Result = "ERROR", Message = ex.Message });
如何在 mvc3 中获取相关表名?可以使用linq吗?