【发布时间】:2009-04-06 16:03:26
【问题描述】:
我创建了两个表:
出版商
- PK, PublisherId, int, not null, indentity +1
- PublisherName,varchar(50),不为空
产品
- PK, ProductId, int, not null, identity +1
- 产品名称,varchar(50),不为空
- PublisherId,int,不为空
然后我在连接 PublisherId 的那些表之间创建了一个外键约束 FK__Product__Publisher。
这样,我想支持每个都有一个发布者的产品,而每个发布者可以有多个产品。就这么简单。
现在我用 C# 创建了一个控制台项目,添加了一个类型化数据集和两个 TableAdapter。一个指向 Publisher 一个指向 Product。 DataSet-Designer 根据从 SQL Server 获得的内容自动添加关系。
它还会自动生成属性,允许我访问选定产品的发布者和选定发布者的所有产品。
这是我的测试代码:
ProductTableAdapter adapter = new ProductTableAdapter();
foreach (ProductRow product in adapter.GetData())
{
Console.WriteLine(product.PublisherRow.PublisherName);
}
但是,这不起作用。使用生成的代码,PublisherRow 属性如下所示:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public LanguageRow PublisherRow {
get {
return ((PublisherRow)(this.GetParentRow(this.Table.ParentRelations["FK_Product_Publisher"])));
}
set {
this.SetParentRow(value, this.Table.ParentRelations["FK_Product_Publisher"]);
}
}
this.Table.ParentRelations 不包含关系。它为 null,访问它会导致 NullReferenceException。
我在这里做错了什么?
【问题讨论】:
标签: strongly-typed-dataset dataset-designer