Reputation: 29427
I have the following entities
and I am trying to map them with help of FluentNHibernate.
Some notes about the entities:
CreatedBy
and AssignedTo
on the Task
entity are of type Person and, on the database side, are int columns called respectively CreatorID
and AssigneeID
WrittenBy
on the entity Note
is of type Person and it persist on the database on a column called AuthorID
I am not going to include all the mapping here as it is maybe not needed, but the biggest problem I am facing to came out when I have added the Task and the Note mapping.
Now in fact if I try to add an Address to a Person object NHibernate try to execute the following query
UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1;
Where am I wrong with this?
EDIT: Added the Mapping of the entities
public PersonMap() {
Table( "Persons" );
Id( c => c.PersonID ).Column( "PersonID" ).GeneratedBy.Identity();
References( c => c.Company ).Column( "CompanyID" );
HasMany( c => c.Addresses ).Cascade.SaveUpdate();
HasMany( c => c.TasksAsCreator ).Cascade.SaveUpdate();
HasMany( c => c.TasksAsAssignee ).Cascade.SaveUpdate();
HasMany( c => c.NotesAsAuthor ).Cascade.SaveUpdate();
}
public TaskMap() {
Table( "Tasks" );
Id( i => i.TaskID ).Column( "TaskID" ).GeneratedBy.Identity();
References( i => i.Company ).Column( "CompanyID" );
References( i => i.CreatedBy ).Column( "CreatorID" );
References( i => i.AssignedTo ).Column( "AssigneeID" );
HasMany( i => i.Notes ).Cascade.SaveUpdate();
}
public NoteMap() {
Table( "Notes" );
Id( n => n.NoteID ).Column( "NoteID" ).GeneratedBy.Identity();
References( n => n.Task ).Column( "TaskID" );
References( n => n.WrittenBy ).Column( "AuthorID" );
}
EDIT 2: After exporting the mapping (thanks to dotjoe) I have found many strange results as the one that follow
<bag cascade="save-update" name="NotesAsAuthor" mutable="true">
<key>
<column name="CreatorID" />
</key>
<one-to-many class="Note, GSLConverter, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</bag>
This is completely wrong! And does not reflect the mapping showed above....
Upvotes: 2
Views: 180
Reputation: 1350
I'm not sure where Fluent nHibernate is getting the CreatorID
column name from but according to the docs you can specify it using the method KeyColumn
HasMany( c => c.NotesAsAuthor ).KeyColumn("AuthorId").Cascade.SaveUpdate();
I don't have a test project in front of me, so give it a go and let me know if it works.
Upvotes: 1
Reputation: 26940
Can you show the mappings for properties related to the Person entity? I would make sure you specify the same fk column names on both sides (Reference and HasMany) of the Note/Task to Person mappings. I've seen this before...I think it's a bug in Fluent when it uses the previously specified column name for a type on another mapping. Also, make sure you have the latest version of Fluent.
To troubleshoot you can view the generated xml mappings with the ExportTo
method of FluentMappingsContainer...
var factory = Fluently.Configure()
.Mappings(mc =>
mc.FluentMappings.AddFromAssemblyOf<PersonMap>().ExportTo("."))
.BuildSessionFactory();
Upvotes: 2