使用 Fluent NHibernate 映射与相应属性具有不同类型的支持字段

发布于 2024-09-10 03:20:38 字数 1314 浏览 3 评论 0原文

我需要使用 Fluent NHibernate 将此类保留在数据库上:

public class RaccoonCity 
{
    public virtual int Id { get; private set; }

    public virtual DateTime InfectionStart { get; private set; }

    private IList<Zombie> _zombies = new List<Zombie>();

    public virtual IEnumerable<Zombie> Zombies
    {
        get { return _zombies; }
    } 
    protected RaccoonCity()
    {}

    public RaccoonCity(DateTime startMonth)
    {
        InfectionStart = startMonth;
    }

    public virtual void AddZombie(Zombie z)
    {
        _zombies.Add(z);
    }

}

该属性的类型为 IEnumerable,表明您不应使用它来插入新项目。支持字段是 IList,以便可以轻松地从自己的类中插入新项目。

Zombie 是一个简单的类:

public class Zombie
{
    public virtual int Id { get; private set; }
    public virtual string FormerName { get; set; }
    public virtual DateTime Infected { get; set; }
}

映射如下:

public class RaccoonCityMap: ClassMap<RaccoonCity> 
{
    public  RaccoonCityMap()
    {
        Id(x => x.Id);
        Map(x => x.InfectionStart);
        HasMany(x => x.Zombies)
            .Access.CamelCaseField(Prefix.Underscore)
            .Inverse()
            .Cascade.All();
    }
}

当我测试它时,数据被插入到数据库中,但僵尸的外键为空,并且 RaccoonCity 实例在 Zombies 列表上有零个项目。

I need to persist this class on database using Fluent NHibernate:

public class RaccoonCity 
{
    public virtual int Id { get; private set; }

    public virtual DateTime InfectionStart { get; private set; }

    private IList<Zombie> _zombies = new List<Zombie>();

    public virtual IEnumerable<Zombie> Zombies
    {
        get { return _zombies; }
    } 
    protected RaccoonCity()
    {}

    public RaccoonCity(DateTime startMonth)
    {
        InfectionStart = startMonth;
    }

    public virtual void AddZombie(Zombie z)
    {
        _zombies.Add(z);
    }

}

The property has type IEnumerable to indicate that you shouldn´t use it to insert new items. The backing field is of IList to make it easy to insert new items from the own class.

Zombie is a simple class:

public class Zombie
{
    public virtual int Id { get; private set; }
    public virtual string FormerName { get; set; }
    public virtual DateTime Infected { get; set; }
}

The map is the following:

public class RaccoonCityMap: ClassMap<RaccoonCity> 
{
    public  RaccoonCityMap()
    {
        Id(x => x.Id);
        Map(x => x.InfectionStart);
        HasMany(x => x.Zombies)
            .Access.CamelCaseField(Prefix.Underscore)
            .Inverse()
            .Cascade.All();
    }
}

When I test this, the data is inserted in database, but the zombie´s foreign keys are empty, and the RaccoonCity instance has zero items on Zombies list.

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

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

发布评论

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

评论(2

2024-09-17 03:20:38

您将这种关系声明为Inverse,这意味着Zombie 而不是RacoonCity 负责维护这种关系。

添加对僵尸的相应引用并将其设置在 AddZombie 方法上,或者删除 Inverse (在这种情况下,您将看到一个带有 null FK 的 INSERT,后跟更新)。

建议阅读: http://nhibernate.info/doc/nh/en /index.html#collections-onetomany

You are declaring the relationship as Inverse, which means the Zombie and not the RacoonCity is responsible for maintaining the relationship.

Either add the corresponding reference to zombie and set it on the AddZombie method, or remove the Inverse (in that case, you'll see an INSERT with a null FK followed by an update).

Suggested reading: http://nhibernate.info/doc/nh/en/index.html#collections-onetomany

非要怀念 2024-09-17 03:20:38

找到了一篇关于它的文章:https://web.archive.org/web/20090831052429/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/ 08/15/a- Fluent-interface-to-nhibernate-part-3-mapping.aspx

我必须实现这个方法
HasManyComponent 是我自己的
在实际的后备箱中丢失了
框架。也就是说,它不是
可以映射值的集合
对象。但这并没有那么难
因为源码库非常好。
我的更改可能会被集成
很快就会进入框架。

还有这个:

http://nhforge.org/blogs/nhibernate/archive/2008/09/06/a- Fluent-interface-to-nhibernate-part-3-mapping-relations.aspx

Found a post about it: https://web.archive.org/web/20090831052429/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/08/15/a-fluent-interface-to-nhibernate-part-3-mapping.aspx

I had to implement the method
HasManyComponent by myself since it
was missing in the actual trunk of the
framework. That is, it was not
possible to map a collection of value
objects. But it has not been that hard
since the source base is really nice.
My changes will probably be integrated
into the framework soon.

And this one:

http://nhforge.org/blogs/nhibernate/archive/2008/09/06/a-fluent-interface-to-nhibernate-part-3-mapping-relations.aspx

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