使用 Fluent NHibernate 映射与相应属性具有不同类型的支持字段
我需要使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将这种关系声明为
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 theZombie
and not theRacoonCity
is responsible for maintaining the relationship.Either add the corresponding reference to zombie and set it on the
AddZombie
method, or remove theInverse
(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
找到了一篇关于它的文章:https://web.archive.org/web/20090831052429/http://blogs.hibernatingrhinos.com/nhibernate/archive/2008/ 08/15/a- Fluent-interface-to-nhibernate-part-3-mapping.aspx
还有这个:
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
And this one:
http://nhforge.org/blogs/nhibernate/archive/2008/09/06/a-fluent-interface-to-nhibernate-part-3-mapping-relations.aspx