开发者

Help needed with FluentNHibernate mapping

开发者 https://www.devze.com 2023-01-24 15:39 出处:网络
I have the following entities and I am trying to map them with help of FluentNHibernate. Some notes about the entities:

I have the following entities

Help needed with FluentNHibernate mapping

and I am trying to map them with help of FluentNHibernate.

Some notes about the entities:

  • The attributes CreatedBy and AssignedTo on the Task entity are of type Person and, on the database side, are int columns called respectively CreatorID and AssigneeID
  • The attribute 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( "Task开发者_运维技巧ID" );
    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....


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();


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消