Fluent nHibernate - 映射字符串列表

发布于 2024-09-13 20:28:18 字数 511 浏览 4 评论 0原文

我有一个这样的模型(简化)

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; }
}

在我的数据库中,我有一个 Post 表和一个 PostAttachment 表

Post Attachment 表有 2 列:

PostID AttachmentKey

(其基本原理是附件上传到亚马逊s3,因此AttachmentKey是s3密钥)

我想要做的是将AttachmentKey映射到返回/插入的Post对象的列表...

我会怎么做关于这样做?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

单调的奢华 2024-09-20 20:28:18

@ben-hughes 你几乎明白了。

您不需要另一个映射。

HasMany(x => x.Attachments)
    .KeyColumn("PostID")
    .Table("PostAttachment").Element("AttachmentKey");

@ben-hughes You almost got it.

You do not need another mapping.

HasMany(x => x.Attachments)
    .KeyColumn("PostID")
    .Table("PostAttachment").Element("AttachmentKey");
月下凄凉 2024-09-20 20:28:18

除非我误解了这个问题,否则就是这样:

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

顺便说一句,Attachments 应该是 IList,而不是 List

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

堇年纸鸢 2024-09-20 20:28:18

我花了一段时间才弄清楚如何使用 Fluent 来做到这一点。实际上非常简单:

public MyClassMapping()
{
    Table("MyClass");

    Id(x => x.Id);

    HasMany(x => x.Strings)
        .Table("MyClassStrings")
        .Element("String");
}

为了支持这一点,您需要创建一个映射表 (MyClassStrings),它有两列:

  • String -外键返回到表 MyClass 的

It took a while for me to work out how to do it with Fluent. It's actually pretty simple:

public MyClassMapping()
{
    Table("MyClass");

    Id(x => x.Id);

    HasMany(x => x.Strings)
        .Table("MyClassStrings")
        .Element("String");
}

To support this, you need to create a mapping table (MyClassStrings) which has two columns:

  • String - the column where the
  • a foreign key back to the table MyClass
爱的故事 2024-09-20 20:28:18

如果只是为了生成表名,您可能需要字符串周围的类型。无论如何,像 List 这样的东西在您的应用程序中可能更有意义。我相信如果需要的话,您可以更深入地挖掘并直接映射到字符串。

从那里您可以从 HasMany 映射和指向您的帖子表的外键开始,即

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

我认为默认情况下这将在您的表中查找 post_ID 列(不需要出现在帖子附件对象上),我如果您需要的话,相信也有办法解决这个问题。

您可能还需要在映射上使用 .Inverse(),具体取决于您希望如何保存帖子附件。

编辑:在看到diego的帖子后,我认为如果PostAttachments是一个字符串列表,上面的方法可能会起作用。我已经使用了他在前几天发布的方法,并且我很确定 HasMany 默认映射到 nhibernate 包。不过,您可能需要在映射中指定列名称才能使用现有表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文