一对多,子相关实体中没有反向引用

发布于 2024-11-10 04:52:56 字数 865 浏览 0 评论 0原文

如果我在两个实体之间有一对多关系(即 PostComment),并且将我的 master 类定义为

public class Post {
    ...
    IList<Comment> Comments { get; set; }
}

Comment 子相关类没有 Post 类型的属性,因为永远不需要从评论到帖子。评论始终与主帖​​子实例一起显示。

然后我有一个存储过程返回两个结果集:与它们相关的帖子和​​评论。我将我的 MapResultSet 定义为

MapResultSet[] sets = new MapResultSet[2];

sets[0] = new MapResultSet(typeof(Post), posts);
sets[1] = new MapResultSet(typeof(Comment));

sets[0].AddRelation(sets[1], /* what goes here? */, "PostID", "Comments");

但这不起作用,因为 Comment 没有对其 Post 的引用,因此我没有为上面代码中的第二个参数定义的任何内容。如果我提供 string.Emptynull 如果方法参数无效,我会收到异常。

在不向 Comment 添加 Post 属性的情况下,我应该如何定义这两个实体之间的关系?

If I have one-to-many relation between two entities (ie. Post and Comment) and have my master class defined as:

public class Post {
    ...
    IList<Comment> Comments { get; set; }
}

But my Comment sub-related class doesn't have a property of type Post, because there's never a need to get from comment to post. Comments are always displayed along with the master post instance.

Then I have a stored procedure that returns two result sets: posts and comments that are related to them. I define my MapResultSet as

MapResultSet[] sets = new MapResultSet[2];

sets[0] = new MapResultSet(typeof(Post), posts);
sets[1] = new MapResultSet(typeof(Comment));

sets[0].AddRelation(sets[1], /* what goes here? */, "PostID", "Comments");

But this doesn't work, since Comment doesn't have a reference to its Post hence I don't have anything to define for the second parameter in the upper code. If I provide string.Empty or null I get an exception if invalid method parameter.

How should I define relationship between these two entities without adding a Post property to Comment?

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

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

发布评论

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

评论(1

姐不稀罕 2024-11-17 04:52:56

评论应该有“PostId”,如果没有,就不可能定义评论的帖子(例如,在结果集中我们有 2 个帖子和 7 个评论),

因此您应该将 Post 属性或 PostId 添加到 Comment 类。

另外,对于简单的情况,请参阅 RelationAttribute,示例位于此处
请参阅 Test2(),这有助于避免手动准备关系,无需编写:

sets[0].AddRelation(sets[1], "PostID", "PostID", "Comments");

Comment should have "PostId", if it doesn't it is impossible to define the post for the comment (for example in result sets we have 2 posts and 7 comments)

So you should add either Post property or PostId to the Comment class.

Also for simple cases please see the RelationAttribute, the example is here
See Test2(), this helps to avoid manually relations prepare, no need to write:

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