Code first 设置两个之间的一对多关系 table
Code first Setting up One to Many Relation between Two table
我有以下 类 代表精确的 table 字段。建立关系的正确方法是什么,以便我可以使用实体框架工作查询相关字段。
我想使用数据注释并希望明确指定关系而不是使用基于约定的方式。
每个组可以有很多技能。
var SkillsGrouped = da.sv_SkillGroups.Include("Sv_Skills");
我收到错误,其中提到无法映射字段名称,实际上它抱怨的字段不是 Table.
的一部分
[Table("sv_Skill")]
public class sv_Skill
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillDescription { get; set; }
public int? skillGroupId { get; set; } //References id column in sv_SkillGroup table
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
}
[Table("sv_SkillGroup")]
public class sv_SkillGroup
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillGroupDescription { get; set; }
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
public virtual ICollection<sv_Skill> sv_Skills { get; set; }
}
我认为您在 class 处没有正确设置 many-to-one 关系的一个端点
sv_Skill声明,更正如下:
[Table("sv_Skill")]
public class sv_Skill
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillDescription { get; set; }
public int? skillGroupId { get; set; }
//this new field was added - link to sv_SkillGroup as foreign key via
//field skillGroupId above
public sv_SkillGroup skillGroup { get; set; }
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
}
使用此表删除当前迁移并使用新代码重新创建它。
我有以下 类 代表精确的 table 字段。建立关系的正确方法是什么,以便我可以使用实体框架工作查询相关字段。
我想使用数据注释并希望明确指定关系而不是使用基于约定的方式。 每个组可以有很多技能。
var SkillsGrouped = da.sv_SkillGroups.Include("Sv_Skills"); 我收到错误,其中提到无法映射字段名称,实际上它抱怨的字段不是 Table.
的一部分[Table("sv_Skill")]
public class sv_Skill
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillDescription { get; set; }
public int? skillGroupId { get; set; } //References id column in sv_SkillGroup table
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
}
[Table("sv_SkillGroup")]
public class sv_SkillGroup
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillGroupDescription { get; set; }
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
public virtual ICollection<sv_Skill> sv_Skills { get; set; }
}
我认为您在 class 处没有正确设置 many-to-one 关系的一个端点 sv_Skill声明,更正如下:
[Table("sv_Skill")]
public class sv_Skill
{
[Required]
[Key]
public int id { get; set; }
[MaxLength(4000)]
public string skillDescription { get; set; }
public int? skillGroupId { get; set; }
//this new field was added - link to sv_SkillGroup as foreign key via
//field skillGroupId above
public sv_SkillGroup skillGroup { get; set; }
[MaxLength(255)]
public string createdBy { get; set; }
public DateTime? createdOn { get; set; }
[MaxLength(255)]
public string updatedBy { get; set; }
public DateTime? updatedOn { get; set; }
[MaxLength(50)]
public string status { get; set; }
}
使用此表删除当前迁移并使用新代码重新创建它。