开发者

Yet Another Fluent Nhibernate Mapping question

开发者 https://www.devze.com 2023-04-04 18:40 出处:网络
I\'m having a hard time figuring out how to handle a mapping situation here: I have different objects (let\'s say news and posts) that can have multiple comments. I en开发者_如何学God up with the fol

I'm having a hard time figuring out how to handle a mapping situation here:

I have different objects (let's say news and posts) that can have multiple comments. I en开发者_如何学God up with the following schema:

NEWS : ID(int), CONTENT(varchar)
POST : ID(int), SUBJECT(varchar)
COMMENT : ID(int), TEXT(varchar)
NEWS_COMMENT : NEWS_ID(int), COMMENT_ID(int)
POST_COMMENT : POST_ID(int), COMMENT_ID(int)

I would like my News and Post objects to have an IList/ICollection Comments property.

How should I go and map this so that I can directly interact with my comments within a post or a news instance?

Note: I have currently created NewsMap, PostMap, CommentMap, NewsCommentMap, PostCommentMap the latter two having compositeids... But it's not working right...

Thanks.


There is no need to create mappings for NEWS_COMMENT and POST_COMMENT. These are many to many relationships and can be mapped within each side of the relationship (NewsMap and PostMap). Please see the example below:

public class NewsMap: ClassMap<News>
{
  public NewsMap()
  {
    Table("NEWS");
    Id(x => x.Id);

    HasManyToMany(x => x.Comments)
     .Table("NEWS_COMMENT")
     .ParentKeyColumn("NEWS_ID")
     .ChildKeyColumn("COMMENT_ID")
     .Cascade.All()
     .Inverse();
  }
}


option1: only have one Comment Class

tables

NEWS : ID(int), CONTENT(varchar)
POST : ID(int), SUBJECT(varchar)
COMMENT : ID(int), TEXT(varchar), NEWS_ID(int), POST_ID(int)

maps

public NewsMap()
{
    HasMany(x => x.Comments)
        .KeyColumn("NEWS_ID")
        .Cascade.AllDeleteOrphan();
}

public PostMap()
{
    HasMany(x => x.Comments)
        .KeyColumn("POST_ID")
        .Cascade.AllDeleteOrphan();
}

or have subclasses

tables

NEWS : ID(int), CONTENT(varchar)
POST : ID(int), SUBJECT(varchar)
COMMENT : ID(int), TEXT(varchar)
NEWS_COMMENT : NEWS_ID(int), COMMENT_ID(int)
POST_COMMENT : POST_ID(int), COMMENT_ID(int)

class Post
{
    public virtual ICollection<PostComment> { get; set; }
}

public PostMap()
{
    HasMany(x => x.Comments)
        .KeyColumn("POST_ID")
        .Cascade.AllDeleteOrphan();
}

public PostCommentMap : SubClassMap<PostComment>
{
    public PostCommentMap()
    {
        Table("POST_COMMENT");
        KeyColumn("COMMENT_ID");
    }
}
0

精彩评论

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

关注公众号