4

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?

Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404

1 Answers1

2

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");
ili
  • 722
  • 1
  • 4
  • 15
  • Actually you're right. My code was returning a single post with comments. But multi resultset can't be aware of it. So I suppose you're answer should be accepted. :) – Robert Koritnik Jun 03 '11 at 12:24