表T_Person: 学生表 有一个外键关键班级表
表T_Class:班级表
[Table(Name = "T_Class")]
public class Class
{
private int _classid;
private string _classname;
private EntitySet<Person> _persons;
[Column(IsPrimaryKey=true)]
public int ClassTypeID
{
get { return _classid; }
set
{
if (_classid != value)
_classid = value;
}
}
[Column]
public string ClassName
{
get { return _classname; }
set
{
if (_classname != value)
_classname = value;
}
}
[Association(Storage="_persons",OtherKey="ClassTypeID")]
public EntitySet<Person> Persons
{
get { return _persons; }
set
{
this._persons.Assign(value);
}
}
}
{
private int _classid;
private string _classname;
private EntitySet<Person> _persons;
[Column(IsPrimaryKey=true)]
public int ClassTypeID
{
get { return _classid; }
set
{
if (_classid != value)
_classid = value;
}
}
[Column]
public string ClassName
{
get { return _classname; }
set
{
if (_classname != value)
_classname = value;
}
}
[Association(Storage="_persons",OtherKey="ClassTypeID")]
public EntitySet<Person> Persons
{
get { return _persons; }
set
{
this._persons.Assign(value);
}
}
}
班级表里面做了一个一对多的关联,就是根据班级ID才获取这个班级下面有多少学生
[Table(Name = "T_Class")]
public class Class
{
private int _classid;
private string _classname;
private EntitySet<Person> _persons;
[Column(IsPrimaryKey=true)]
public int ClassTypeID
{
get { return _classid; }
set
{
if (_classid != value)
_classid = value;
}
}
[Column]
public string ClassName
{
get { return _classname; }
set
{
if (_classname != value)
_classname = value;
}
}
[Association(Storage="_persons",OtherKey="ClassTypeID")]
public EntitySet<Person> Persons
{
get { return _persons; }
set
{
this._persons.Assign(value);
}
}
public class Class
{
private int _classid;
private string _classname;
private EntitySet<Person> _persons;
[Column(IsPrimaryKey=true)]
public int ClassTypeID
{
get { return _classid; }
set
{
if (_classid != value)
_classid = value;
}
}
[Column]
public string ClassName
{
get { return _classname; }
set
{
if (_classname != value)
_classname = value;
}
}
[Association(Storage="_persons",OtherKey="ClassTypeID")]
public EntitySet<Person> Persons
{
get { return _persons; }
set
{
this._persons.Assign(value);
}
}
}
这里做了一个多对一的关联 关联班级对象,用于查询班级的名称或其他信息
Table<Person> Pers = DB.GetTable<Person>();
foreach (var v in Pers)
Console.WriteLine(v.Class.ClassName);
Table<Class> Class=DB.GetTable<Class>();
foreach (var v in Class)
foreach (var v in Pers)
Console.WriteLine(v.Class.ClassName);
Table<Class> Class=DB.GetTable<Class>();
foreach (var v in Class)
Console.WriteLine(v.Persons.Count);
以上是使用方法