how to map SubclassMap and HasManyToMany in Fluent NHibernate
- by Davide Orazio Montersino
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.