how to map SubclassMap and HasManyToMany in Fluent NHibernate
Posted
by Davide Orazio Montersino
on Stack Overflow
See other posts from Stack Overflow
or by Davide Orazio Montersino
Published on 2010-05-03T14:57:16Z
Indexed on
2010/05/05
12:38 UTC
Read the original article
Hit count: 842
Hi everyone. 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.
© Stack Overflow or respective owner