代码改变世界

Visual Entity 使用(三)创建多对多关系

2011-08-24 18:03  麦舒  阅读(504)  评论(0编辑  收藏  举报

1、将表 Employees、EmployeeTerritories、Territories 三个表拖放到设计器上。如下图所示:

 

2、右键点击 EmployeeTerritory 实体类,在弹出的菜单中,选择 Convet to M:M Association 菜单项。

 

 

3、运行代码

staticvoid Main(string[] args)
{
var dc
=new NorthwindDataContext();
var territories
= dc.Territories;
foreach (var territory in territories)
{
foreach (var employee in territory.Employees)
{
Console.WriteLine(employee.FirstName
+""+ employee.LastName);
}
Console.WriteLine();
}

var employees
= dc.Employees;
foreach(var employee in employees)
{
foreach(var territory in employee.Territories)
{
Console.WriteLine(territory.TerritoryDescription);
}
}
}

 

4、结果如下图所示