一对多,子相关实体中没有反向引用
如果我在两个实体之间有一对多关系(即 Post
和 Comment
),并且将我的 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.Empty
或 null
如果方法参数无效,我会收到异常。
在不向 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
评论应该有“PostId”,如果没有,就不可能定义评论的帖子(例如,在结果集中我们有 2 个帖子和 7 个评论),
因此您应该将 Post 属性或 PostId 添加到 Comment 类。
另外,对于简单的情况,请参阅 RelationAttribute,示例位于此处
请参阅 Test2(),这有助于避免手动准备关系,无需编写:
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: