SqlSugar/Src/Asp.Net/SqlServerTest/Demos/6_ComplexModel.cs

81 lines
2.1 KiB
C#
Raw Normal View History

2017-06-11 11:27:39 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SqlSugar;
using OrmTest.Demo;
namespace OrmTest.Demo
{
public class ComplexModel : DemoBase
{
public static void Init()
{
var db = GetInstance();
2018-05-03 16:22:18 +08:00
db.Insertable(new CMStudent() { SchoolId = 1, Name = "xx1" }).ExecuteCommand();
2017-06-11 14:15:24 +08:00
var students = db.Queryable<CMStudent>().ToList();
2017-06-11 14:26:01 +08:00
if (students != null)
{
foreach (var item in students)
{
Console.WriteLine(item.SchoolName);
if (item.SchoolSingle != null)
{
Console.WriteLine(item.SchoolSingle.Name);
}
if (item.SchoolList != null)
{
Console.WriteLine(item.SchoolList.Count);
}
}
}
2018-03-05 16:15:19 +08:00
db.Insertable(new CMStudent() { Name="xx" }).ExecuteCommand();
2017-06-11 11:27:39 +08:00
}
}
[SugarTable("Student")]
public class CMStudent : ModelContext
{
public int Id { get; set; }
public string Name { get; set; }
public int SchoolId { get; set; }
2017-06-11 14:15:24 +08:00
[SugarColumn(IsIgnore = true)]
public string SchoolName
{
get
{
if (this.SchoolSingle != null)
return this.SchoolSingle.Name;
else
return null;
}
}
2017-06-11 11:27:39 +08:00
[SugarColumn(IsIgnore = true)]
public CMSchool SchoolSingle
{
get
{
2017-06-11 14:15:24 +08:00
return base.CreateMapping<CMSchool>().Single(it => it.Id == this.SchoolId);
2017-06-11 11:27:39 +08:00
}
}
[SugarColumn(IsIgnore = true)]
public List<CMSchool> SchoolList
{
get
{
2017-06-11 14:15:24 +08:00
return base.CreateMapping<CMSchool>().Where(it => it.Id == this.SchoolId).ToList();
2017-06-11 11:27:39 +08:00
}
}
}
[SugarTable("School")]
public class CMSchool
{
public int Id { get; set; }
public string Name { get; set; }
}
}