如何使用SMO
在两个方向上查询与表相关的表例如
Table Employee
.....
JobId int NULL -- FK
Table Job
JobId int NOT NULL -- PK
使用某种生成工具,如MS T4 Template和SMO;我想要达到以下课程
class Employee {
......
public int? Job_Id
{
get { return _Job_Id; }
set
{
if (_Job_Id!= value)
{
if (Job != null && Job.JobId != value) //THE PROBLEM HERE (I CANNOT GET THE RELATED PK FOR THE RELATED TABLE)
{
Job = null;
}
_Job_Id= value;
}
}
}
private int? _Job_Id;
Job TheJob {get; set;}
}
class Job {
.....
ICollection<Employee> Employees { ..... }
}
以下是我如何获得相关表格
public ArrayList GetChildren(Table tbl, Server server)
{
var result = new ArrayList();
DependencyWalker w = new DependencyWalker(server);
DependencyTree tree = w.DiscoverDependencies(new SqlSmoObject[]{tbl}, DependencyType.Children);
DependencyCollection depends = w.WalkDependencies(tree);
foreach (DependencyCollectionNode dcn in depends)
{
//System.Windows.Forms.MessageBox.Show( tbl.Name + " -> " + dcn.Urn.Parent);
if (dcn.Urn.Type == "Table" && dcn.Urn.GetNameForType("Table") != tbl.Name)
{
result.Add(dcn.Urn.GetNameForType("Table"));
}
}
return result;
}
但是如何在相关表中获取引用的主键(相关表的PK,我是FK)。