views:

49

answers:

2

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?

A: 

You may need a type around the string, if only to be able to generate a table name. Something like List may be more meaningful in your application anyways. I'm sure you can dig in deeper and map directly to the string if you need to.

From there you can start with a HasMany mapping and a foreign key pointing to your post table, ie

HasMany (o => o.PostAttachments).ForeignKeyConstraintName ("FK_Attachment_Post");

I think by default this will look for a post_ID column in your table (doesn't need to be present on the post attachment object), I'm sure there is a way around this too if you need it.

You may need a .Inverse() on the mapping as well, depending on how you want the Post Attachments to be saved.

edit: after seeing diego's post, I think the above may just work, if PostAttachments is a list of strings. I've used the method he posted in pre fluent days, and I'm pretty sure HasMany maps to an nhibernate bag by default. You'll probablly need to specify column names in your mapping to use existing table though.

AlexCuse
+2  A: 

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>.

Diego Mijelshon
Oh, I see you're using fluent. Well, you'll have to find what's the equivalent for mapping collections of values.
Diego Mijelshon