如何在 Fluent NHibernate 中映射 SubclassMap 和 HasManyToMany
我的问题是流畅的 nhibernate 映射多对多关系,它们最终引用了一个不存在的 Id。
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Password);
Map(x => x.Confirmed);
HasMany(x => x.Nodes).Cascade.SaveUpdate();
HasManyToMany<Node>(x => x.Events).Cascade.SaveUpdate().Table("RSVPs");
}
public EventMap()
{
Map(x => x.Starts);
Map(x => x.Ends);
HasManyToMany<User>(x => x.Rsvps).Cascade.SaveUpdate().Table("RSVPs");
}
public NodeMap() {
Id(x => x.Id);
Map(x => x.Title);
Map(x => x.Body).CustomSqlType("text");
Map(x => x.CreationDate);
References(x => x.Author).Cascade.SaveUpdate();
Map(x => x.Permalink).Unique().Not.Nullable();
}
这些是我的类 - 请注意 Event 继承自 Node:
public class Event : Node//, IEvent
{
private DateTime _starts = DateTime.MinValue;
private DateTime _ends = DateTime.MaxValue;
public virtual IList<User> Rsvps { get; set; }
}
问题是,生成的 RSVP 表如下所示:
Event_id 用户身份 Node_id
当然,事件表没有 ID - 只有 Node_id。
当尝试保存关系时,它将尝试保存 NULL event_id,从而生成错误。
My problem is fluent nhibernate mapping a many to many relationship, they end up referencing a non existent Id.
public UserMap()
{
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.Password);
Map(x => x.Confirmed);
HasMany(x => x.Nodes).Cascade.SaveUpdate();
HasManyToMany<Node>(x => x.Events).Cascade.SaveUpdate().Table("RSVPs");
}
public EventMap()
{
Map(x => x.Starts);
Map(x => x.Ends);
HasManyToMany<User>(x => x.Rsvps).Cascade.SaveUpdate().Table("RSVPs");
}
public NodeMap() {
Id(x => x.Id);
Map(x => x.Title);
Map(x => x.Body).CustomSqlType("text");
Map(x => x.CreationDate);
References(x => x.Author).Cascade.SaveUpdate();
Map(x => x.Permalink).Unique().Not.Nullable();
}
Those are my classes -notice that Event inherits from Node:
public class Event : Node//, IEvent
{
private DateTime _starts = DateTime.MinValue;
private DateTime _ends = DateTime.MaxValue;
public virtual IList<User> Rsvps { get; set; }
}
The problem is, the generated RSVPs table is like that:
Event_id
User_id
Node_id
Of course the Event table has no ID - only a Node_id.
When trying to save a relationship it will try to save a NULL event_id thus generating an error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我应该更了解流畅的 NHibernate:经过一些尝试和错误,一切正常。但是,我应该加深我的知识。这是工作代码:(
节点和事件类保持不变)
我必须指定:
EventMap Hope 这可以帮助某人作为 HasManyToMany / SubclassMap 示例。
Ok, I should have known fluent NHibernate better: after some trial and error, everything works. However, I should deepen my knowledge. This is the working code:
(The node and event class remain the same)
I had to specify:
Hope this can help someone as a HasManyToMany / SubclassMap example.