开发者

One-to-Many with no back reference in sub-related entity

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?


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");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜