nhibernate Fluent 映射复合 ID
我正在学习使用流畅的 nhibernate,但无法理解如何创建映射。我有一个具有多列主键的表,但我无法正确映射它。我有以下流畅的映射 -
public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
{
public void Override(AutoMapping<MyEntity> mapping)
{
mapping.CompositeId()
.KeyProperty(x => x.Id_1, "Id_1")
.KeyProperty(x => x.Id_2, "Id_2");
mapping.References(x => x.otherEntity)
.Column("JoinColumn");
// Commented out to attempt to map to another entity on multiple columns
//mapping.HasMany(x => x.thirdEntit)
// .KeyColumns.Add("thirdId_1", "thirdId_2")
// .Cascade.All();
}
}
我遇到的问题是复合 id 似乎不起作用。
这是生成的映射文件中的一个片段 -
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
然后我收到一个错误,指出在查询的任何表中都找不到列 (id)。
我是否错误地认为我的映射代码会生成正确的复合 ID?我是否遗漏了什么或做错了什么?
I am learning to use fluent nhibernate and am having trouble understanding how to create my mappings. I have a table that has a multi-column primary key, but I can't get it to map correctly. I have the following fluent mapping -
public class MyEntityMappingOverride : IAutoMappingOverride<MyEntity>
{
public void Override(AutoMapping<MyEntity> mapping)
{
mapping.CompositeId()
.KeyProperty(x => x.Id_1, "Id_1")
.KeyProperty(x => x.Id_2, "Id_2");
mapping.References(x => x.otherEntity)
.Column("JoinColumn");
// Commented out to attempt to map to another entity on multiple columns
//mapping.HasMany(x => x.thirdEntit)
// .KeyColumns.Add("thirdId_1", "thirdId_2")
// .Cascade.All();
}
}
The problem that I am having is the composite id doesn't seem to be working.
Here is a snippet from the produced mapping file -
<id name="Id" type="System.Int32, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<column name="Id" />
<generator class="identity" />
</id>
I then get an error that column (id) is not found in any table in the query.
Am I wrong in thinking that my mapping code will produce the correct composite ID? Am I missing something or doing something incorrectly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
建议(并且我已经取得了很多成功)如果您正在执行 CompositeKeys,请按照以下方式使用 ID 对象:
NHibernate 和复合键
在您的情况下,您将创建一个新类...
然后您的映射变为
您的类来自具有两个属性(Id_1 和 Id_2),而仅具有 MyEntityIdentifier。
然后,您在需要加载的地方使用 MyEntityIdentifier。
而且,如果您对此数据库有任何控制权,我强烈建议不要使用 CompositeKeys。虽然 NHibernate 肯定会处理它们,但它们确实带来了一些额外的心理复杂性和问题。
It's recommended (and I've had lots of success with) that if you're doing CompositeKeys you use an ID object as per:
NHibernate and Composite Keys
In your case you'd create a new class...
Then your mapping becomes
And your class goes from having two properties (Id_1 & Id_2) to just having a MyEntityIdentifier.
Then you use your MyEntityIdentifier where required to load..
And also if you have ANY control over this database I would STRONGLY recommend not using CompositeKeys. While NHibernate will definitely handle them they do come with a bit of additional mental complexity and problems.