1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.ComponentModel.DataAnnotations; 6 using System.Data.Entity; 7 8 namespace Mydream.Entity 9 { 10 [Table("tb_board")] 11 public class BoardEntity 12 { 13 [Key] 14 public int BoardID { set; get; } 15 16 [Required,MaxLength(255)] 17 public string Title { set; get; } 18 19 [Required, MaxLength(500)] 20 public string Keywords { set; get; } 21 22 [Required, MaxLength(500)] 23 public string Desc { set; get; } 24 25 public int Sort { set; get; } 26 27 public bool IsDisplay { set; get; } 28 29 public bool IsDelete { set; get; } 30 31 [Required, Column(TypeName = "smalldatetime")] 32 public DateTime AddDate { set; get; } 33 34 public virtual ICollection<BoardItemEntity> ItemBoardEntitys { set; get; } 35 } 36 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using System.Data.Entity; namespace Mydream.Entity { [Table("tb_board_item")] public class BoardItemEntity { [Key] public int BoardItemID { set; get; } public int BoardID { set; get; } [MaxLength(255)] public string Title { set; get; } [MaxLength(500),Column(TypeName="varchar")] public string Photo { set; get; } [Required,MaxLength(500)] public string Keywords { set; get; } [Required, MaxLength(500)] public string Desc { set; get; } public int Sort { set; get; } public bool IsDisplay { set; get; } public bool IsDelete { set; get; } [Required, Column(TypeName = "smalldatetime")] public DateTime AddDate { set; get; } } }
我获取 IEnumerable<BoardEntity>的时候,如何查询出BoardItemEntitys
using (var context = new TKInfDBContext())
{
return (from m in context.Dic_POPValueDef.Include("Dic_POPTypeDef").Include("Dic_POPTypeDef.Dic_POPClassDef")
where m.VID == vid
select m).FirstOrDefault();
}
这是使用DBContext来实现的一对多的查询。不过是Linq to entity 的方式。Linq To Sql的写法,还真没试过。