属性导航


该功能是懒加载模式性能比较差,新版本建议使用 Mapper实现 http://www.codeisbug.com/Doc/8/1170



定义复杂模型需要注意2点:

一、属性上面需要加上IsIgnore = true

二、实体类需要继承 ModelContext


[SugarTable("Student")]
public class CMStudent : ModelContext
{
    public int Id { get; set; }
    
    public string Name { get; set; }
    
    public int SchoolId { get; set; }
    
    [SugarColumn(IsIgnore = true)]
    public string SchoolName{get{ return this.SchoolSingle.Name;}}

    [SugarColumn(IsIgnore = true)]
    public CMSchool SchoolSingle
    {
        get
        {
         return base.CreateMapping<CMSchool>().Single(it => it.Id == this.SchoolId);
        }
    }

    [SugarColumn(IsIgnore = true)]
    public List<CMSchool> SchoolList
    {
        get
        {
         return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
        }
    }
}

[SugarTable("School")]
public class CMSchool
{
    public int Id { get; set; }
    public string Name { get; set; }
}


使用复杂的MODEL多表查询

  var students = db.Queryable<CMStudent>().ToList();
  if (students != null)
  {
       foreach (var item in students)
       {
            Console.WriteLine(item.SchoolName);

            Console.WriteLine(item.SchoolSingle.Name);
            
            Console.WriteLine(item.SchoolList.Count);
    }
  }



收藏到朋友圈: