该功能是懒加载模式性能比较差,新版本建议使用 Mapper实现 http://www.donet5.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; }
} 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);
}
}2016 © donet5.comApache Licence 2.0