ParentRelations 在生成的数据集中不起作用?

发布于 2024-07-09 17:41:32 字数 1380 浏览 8 评论 0原文

我创建了两个表:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别在捏我脸啦 2024-07-16 17:41:32

回答我自己的帖子似乎有点奇怪。 这有点像自言自语。 然而,我找到了一个解决方案,并且由于这个问题似乎很常见,所以我将其发布在这里。

简单地使用 TableAdapter 获取数据不会从数据库中检索正确的关系。 关系的初始化发生在数据集类中(即从数据集的 c'tor 调用的 InitClass 方法):

public DbDataSet() {
    this.BeginInit();
    this.InitClass();
    ...
    this.EndInit();
}

相应生成的 InitClass 方法如下所示:

private void InitClass() {
    ...
    this.relationFK_Product_Publisher= new global::System.Data.DataRelation("FK_Product_Publisher", new global::System.Data.DataColumn[] { this.tableProduct.PublisherIdColumn}, new global::System.Data.DataColumn[] { this.tablePublisher.PublisherIdColumn}, false);
    this.Relations.Add(this.relationFK_Product_Publisher);
}

这就是为什么您需要实例化数据集类本身并使用fill 方法来填充你的表类,如下所示:

DbDataSet ds = new DbDataSet();    

ProductTableAdapter productAdapter = new ProductTableAdapter();            
productAdapter.Fill(ds.Product);

PublisherTableAdapter publisherAdapter = new PublisherTableAdapter();
publisherAdapter.Fill(ds.Publisher);

foreach (ProductRow product in ds.Product)
{                
    Console.WriteLine(product.PublisherRow.PublisherName);
}

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):

public DbDataSet() {
    this.BeginInit();
    this.InitClass();
    ...
    this.EndInit();
}

The corresponding generated InitClass method looks like this:

private void InitClass() {
    ...
    this.relationFK_Product_Publisher= new global::System.Data.DataRelation("FK_Product_Publisher", new global::System.Data.DataColumn[] { this.tableProduct.PublisherIdColumn}, new global::System.Data.DataColumn[] { this.tablePublisher.PublisherIdColumn}, false);
    this.Relations.Add(this.relationFK_Product_Publisher);
}

This is why you need to instantiate the dataset class itself and use the fill method to populate your table classes like this:

DbDataSet ds = new DbDataSet();    

ProductTableAdapter productAdapter = new ProductTableAdapter();            
productAdapter.Fill(ds.Product);

PublisherTableAdapter publisherAdapter = new PublisherTableAdapter();
publisherAdapter.Fill(ds.Publisher);

foreach (ProductRow product in ds.Product)
{                
    Console.WriteLine(product.PublisherRow.PublisherName);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文