Maxtoroq / DBExtensions如何映射子列表?

时间:2014-09-07 16:49:42

标签: .net dbextensions

Using Maxtoroq's DBExtensions how can we map an object having list of elements as child property 
using just one sql builder?

My sample DB structure 

Employee
   EmployeeId
   FirstName
   LastName

Department
 DeptId
 DeptName

EmployeeDepartment
  EmployeeId
  DeptId


My POCO 

class Department{

 public int DeptId {get;set;}

 public string DeptName {get;set;}
}

class Employee {

public void Employee(){
  Depts = new List<Department>();
}

  public int EmployeeId {get;set;}

  public string FirstName {get;set;}

  public string LastName {get;set;}

  public List<Department> Depts {get;set;}
}

现在我想创建User的实例,包括他使用SqlBuilder关联的所有部门,方法是一次性对db执行查询(不是多次调用db)。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

AFAIK,没有可以加载实体的ORM,包括单个数据库调用中的一对多关联。

你可以在DbExtensions中执行此操作,但是使用Database / SqlTable代替SqlBuilder:

class MyDatabase : Database {

   public SqlTable<Employee> Employees {
      get { return Table<Employee>(); }
   }

   public MyDatabase()
      : base("name=MyDatabase") { }
}

var db = new MyDatabase();

Employee emp = db.Employees
   .Include("Depts")
   .Find(1);