ParentRelations 在生成的数据集中不起作用?
我创建了两个表:
Publisher
- PK, PublisherId, int, not null, indentity +1
- PublisherName, varchar(50), not null
Product
- PK, ProductId, int, not null ,identity +1
- ProductName, varchar(50), not null
- PublisherId, int, not null
然后我在连接 PublisherId 的表之间创建了一个外键约束 FK__Product__Publisher 。
通过这种方式,我希望支持每个具有一个发布者的产品,而每个发布者可以拥有多个产品。 就这么简单。
现在我用 C# 创建了一个控制台项目,添加了一个类型化数据集和两个 TableAdapter。 一个指向发布者,一个指向产品。 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。
我在这里做错了什么?
I created two tables:
Publisher
- PK, PublisherId, int, not null, indentity +1
- PublisherName, varchar(50), not null
Product
- PK, ProductId, int, not null, identity +1
- ProductName, varchar(50), not null
- PublisherId, int, not null
Then I created a foreign key constraint FK__Product__Publisher between those tables connecting the PublisherId's.
This way I want to support Products that have one Publisher each while each Publisher can have multiple Products. It's as simple as that.
Now I created a console project in C#, added a typed dataset and two TableAdapter. One pointing to Publisher one to Product. The DataSet-Designer automatically adds the relation based on what it gets from SQL Server.
It also automatically generates properties which should allow me to access the publisher from a selected Product and all products from a selected publisher.
This is my test code:
ProductTableAdapter adapter = new ProductTableAdapter();
foreach (ProductRow product in adapter.GetData())
{
Console.WriteLine(product.PublisherRow.PublisherName);
}
However, this does not work. With the generated code, the property PublisherRow looks like this:
[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 does not contain the relation. It is null and accessing it results in a NullReferenceException.
What am I doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答我自己的帖子似乎有点奇怪。 这有点像自言自语。 然而,我找到了一个解决方案,并且由于这个问题似乎很常见,所以我将其发布在这里。
简单地使用 TableAdapter 获取数据不会从数据库中检索正确的关系。 关系的初始化发生在数据集类中(即从数据集的 c'tor 调用的 InitClass 方法):
相应生成的 InitClass 方法如下所示:
这就是为什么您需要实例化数据集类本身并使用fill 方法来填充你的表类,如下所示:
It seems a little weird answering my own post. It's a little like talking to myself. However, I sort of found a solution and since this problem seems to be pretty common, I post it here.
Simply using the TableAdapters to get the data will not retrieve the proper relations from the database. The initialization of the relationships takes place within the dataset class (namely the InitClass method which is called from the datasets' c'tor):
The corresponding generated InitClass method looks like this:
This is why you need to instantiate the dataset class itself and use the fill method to populate your table classes like this: