Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent nHibernate - Map a list of strings

I have a model such as this (simplified)

public class Post
{
    public string ID { get; set; }    
    public string Title { get; set; }
    public string Body { get; set; }
    public string AuthorName { get; set; }

    public List<string> Attachments { get; set; }
}

In my database, I have a Post table, and a PostAttachment table

Post Attachment table has 2 columns:

PostID AttachmentKey

(The basics of this are that the attachment is uploaded to amazon s3, so the AttachmentKey is the s3 key)

What I want to do is map the AttachmentKey to the List of a returned / inserted Post object...

How would I go about doing this?

like image 557
Alex Avatar asked Aug 10 '10 11:08

Alex


2 Answers

@ben-hughes You almost got it.

You do not need another mapping.

HasMany(x => x.Attachments)
    .KeyColumn("PostID")
    .Table("PostAttachment").Element("AttachmentKey");
like image 160
regisbsb Avatar answered Oct 19 '22 00:10

regisbsb


Unless I misunderstood the question, it's just this:

<bag name="Attachments" table="Attachment">
  <key column="PostId" />
  <element column="AttachmentKey" />
</bag>

BTW, Attachments should be an IList<string>, not List<string>.

like image 44
Diego Mijelshon Avatar answered Oct 19 '22 00:10

Diego Mijelshon