如何使用 .Net Ria 服务与具有相同密钥(“Id”)的对象关联?

发布于 2024-07-26 04:59:30 字数 624 浏览 3 评论 0原文

假设我有两个类,并且要求主键属性必须命名为“Id”(例如:Book 和 Publisher)。 这个要求是因为我正在与我们公司代码生成的对象合作,并且无法更改它们的生成方式。 这些类之间有一个外键:Book 到 Publisher,并且是一对一的关系,在这种情况下,每本书只有一个 Publisher。

为了将 Book.Publisher 用作 .Net RIA 服务中的属性,您需要添加属性。 例如,在 Book 类的元数据中:
[钥匙] 公共 int Id; ... [包括]
[关联(“Book_Publisher”,“Id”,“Id”,IsForeignKey = true)]
公共出版商出版商;

在发布者类的元数据中:
[关键]
公共 int Id;
...

在客户端代码中,我尝试获取发布者: 出版商 booksPublisher = Book.Publisher; 但我得到了错误的发布者(或 null)。 查看数据库后,它要查找的Publisher id是书籍的id,而不是出版商。

我可以在元数据中添加别名吗? RIA 服务的更高版本会处理这个问题吗?

谢谢。

Say I have two classes and have a requirement that the primary key property must be named "Id" (example: Book and Publisher). The requirement is because I'm working with our companies code generated objects and can't change the way they are generated. These classes have a a foreign key between them: Book to Publisher and is a one to one relationship, where in this case, each Book only has one Publisher.

In order to use Book.Publisher as a property in .Net RIA Services, you need to add the attributes.
For example in the meta data for the Book class:
[Key]
public int Id;
...
[Include]
[Association("Book_Publisher", "Id", "Id", IsForeignKey = true)]
public Publisher Publisher;

In the meta data for the publisher class:
[Key]
public int Id;
...

In the client code I attempt to get the Publisher:
Publisher booksPublisher = Book.Publisher;
But I get the wrong publisher (or a null). After looking at the database, the Publisher id is it looking for is the id of the book, not the publisher.

Can I add an alias in the meta data? Will later versions of RIA Services handle this?

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画▽骨i 2024-08-02 04:59:31

只是猜测...但我认为您遇到问题的原因是您尝试将 Book 对象中的 Id 字段键入到 Publisher 对象的 Id 字段。 我猜 Book.Id 唯一标识了这本书,而不是出版商。

Book 对象是否有一个名为publisherId 或类似字段的字段? 如果是这样,您应该使用 Book.publisherId = Publisher.Id 将图书键入到 Publisher。

Just a guess...but I think the reason you're having issues is because you're trying to key the Id field from the Book object to the Id field of the Publisher object. I'm guessing that Book.Id uniquely identifies the book, not the publisher.

Does the Book object have a field called publisherId or something similar? If so, you should be keying the Book to Publisher using Book.publisherId = Publisher.Id.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文