在EntityFrameWork跟数据库映射时出现“列名 'ParentBanKuai_Id' 无效。”等类似问题的时候。 问题原型图:
数据库映射:
实体类:
/// <summary>
/// 父版块表
/// </summary>
[Serializable]
public class ParentBanKuai
{
/// <summary>
/// 父版块编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 父版块名称
/// </summary>
public string Name { get; set; }
}
/// <summary>
/// 子版块表
/// </summary>
[Serializable]
public class ChildBanKuai
{
//此处设置对象为外键 这个是问题的核心所在
[ForeignKey("ParentBanKuai")]
/// <summary>
/// 子版块编号
/// </summary>
public int Id { get; set; }
/// <summary>
/// 子版块名称
/// </summary>
public string Name { get; set; }
public string ParentId { get; set; }
/// <summary>
/// 父版块Id 此处定义成virtual,是为了延迟加载
/// </summary>
public virtual ParentBanKuai ParentBanKuai { get; set; }
}