FluentNHibernate 映射需要帮助
我有以下实体
我正在尝试在 FluentNHibernate 的帮助下映射它们。
关于实体的一些注意事项:
Task
实体上的属性CreatedBy
和AssignedTo
属于 Person 类型,在数据库端是 int 列分别称为CreatorID
和AssigneeID
- 实体
Note
上的属性WrittenBy
是 Person 类型,并且持久保存在数据库中在名为AuthorID
的列上,
我不会在此处包含所有映射,因为可能不需要它,但当我添加任务和注释映射时,我面临的最大问题就出现了。
现在事实上,如果我尝试将地址添加到 Person 对象 NHibernate 尝试执行以下查询
UPDATE Addresses SET AuthorID = @p0 WHERE AddressID = @p1;
我哪里错了?
编辑:添加了实体的映射
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" );
}
编辑2:导出映射后(感谢 dotjoe),我发现了许多奇怪的结果,如下所示
<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>
这是完全错误!并且不反映上面显示的映射......
I have the following entities
and I am trying to map them with help of FluentNHibernate.
Some notes about the entities:
- The attributes
CreatedBy
andAssignedTo
on theTask
entity are of type Person and, on the database side, are int columns called respectivelyCreatorID
andAssigneeID
- The attribute
WrittenBy
on the entityNote
is of type Person and it persist on the database on a column calledAuthorID
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您能否显示与 Person 实体相关的属性的映射?我将确保您在 Note/Task 到 Person 映射的两侧(Reference 和 HasMany)指定相同的 fk 列名称。我以前见过这个...我认为这是 Fluent 中的一个错误,当它使用先前指定的列名称作为另一个映射上的类型时。另外,请确保您拥有最新版本的 Fluent。
要排除故障,您可以使用 FluentMappingsContainer 的
ExportTo
方法查看生成的 xml 映射...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...我不确定 Fluent nHibernate 从哪里获取
CreatorID
列名称,但根据 文档 您可以使用方法KeyColumn
指定它我面前没有测试项目,所以请尝试一下,如果有的话请告诉我有用。
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 methodKeyColumn
I don't have a test project in front of me, so give it a go and let me know if it works.