FluentNHibernate 映射需要帮助

发布于 2024-10-01 18:22:22 字数 2105 浏览 3 评论 0原文

我有以下实体

alt text

我正在尝试在 FluentNHibernate 的帮助下映射它们。

关于实体的一些注意事项:

  • Task 实体上的属性 CreatedByAssignedTo 属于 Person 类型,在数据库端是 int 列分别称为 CreatorIDAssigneeID
  • 实体 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

alt text

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( "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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

℉服软 2024-10-08 18:22:22

您能否显示与 Person 实体相关的属性的映射?我将确保您在 Note/Task 到 Person 映射的两侧(Reference 和 HasMany)指定相同的 fk 列名称。我以前见过这个...我认为这是 Fluent 中的一个错误,当它使用先前指定的列名称作为另一个映射上的类型时。另外,请确保您拥有最新版本的 Fluent。

要排除故障,您可以使用 FluentMappingsContainer 的 ExportTo 方法查看生成的 xml 映射...

var factory = Fluently.Configure()
                .Mappings(mc =>
                    mc.FluentMappings.AddFromAssemblyOf<PersonMap>().ExportTo("."))
                .BuildSessionFactory();

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();
幸福丶如此 2024-10-08 18:22:22

我不确定 Fluent nHibernate 从哪里获取 CreatorID 列名称,但根据 文档 您可以使用方法 KeyColumn 指定它

HasMany( c => c.NotesAsAuthor ).KeyColumn("AuthorId").Cascade.SaveUpdate();

我面前没有测试项目,所以请尝试一下,如果有的话请告诉我有用。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文