如何为 List创建映射在 FluentNhibernate 中?

发布于 2024-08-26 17:47:09 字数 608 浏览 6 评论 0原文

我正在尝试使用 Fluent NHibernate 为以下模型创建映射文件。但是,我不确定如何在映射文件中对 List<string> 进行映射。

    public class MyClass
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual List<string> MagicStrings { get; set; }
    }

    public class EnvironmentMapping : ClassMap<Models.Environment>
    {
        public EnvironmentMapping()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            //HasMany(x => string)  What should this be ?
        }
    }

I am trying to create a mapping file for the following Model using Fluent NHibernate. But, I am not sure of how to do the mapping for the List<<string>string> in the mapping file.

    public class MyClass
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual List<string> MagicStrings { get; set; }
    }

    public class EnvironmentMapping : ClassMap<Models.Environment>
    {
        public EnvironmentMapping()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            //HasMany(x => string)  What should this be ?
        }
    }

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

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

发布评论

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

评论(2

你爱我像她 2024-09-02 17:47:09

这并不完全是您要问的,但我只是想指出 FNH Automapping 将映射您的类,绝对不需要程序员的进一步帮助 - 即您不需要额外的映射类。

您只需将该成员声明为 IList,而不是 List。 (实际上,我认为您也必须使用 IList 进行常规 FNH 映射)。

进一步的一点 - 自动映射值类型(例如字符串和整数)存在一个错误,该错误最近已修复,因此如果您决定采用自动映射路线(我强烈推荐,顺便说一句,请确保您使用的是最新的 FNH 版本) !)。

This is not quite what you are asking, but I just want to point out that FNH Automapping will map your class with absolutely no further help from the programmer - i.e. you don't need additional Mapping classes.

You just have to declare the member as an IList, instead of a List. (Actually, I thought you had to use IList for regular FNH mapping too).

One further point - there was a bug with automapping value types such as strings and ints, which was fixed very recently, so make sure you're using the latest FNH builds if you decide to go the Automapping route (which I highly recommend, BTW!).

§对你不离不弃 2024-09-02 17:47:09

我找到了解决问题的方法,在我的情况下,我必须为 MyStrings 创建一个单独的表,并与 MyClass 建立外键关系。

I found a solution for my problem, in my situation I have to create a separate table for MyStrings and have a foreign key relation with MyClass.

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