CompositeId 导致无法编译映射文档错误
我正在尝试使用 CompositeId 映射到旧系统。源数据库有一个复合主键,因此我无法使用正常的 this.Id 映射。
这是我尝试映射它:
public PriorityListPartMap()
{
this.Schema("EngSchedule");
this.Table("vPriorityListPart");
this.CompositeId().KeyProperty(x => x.AssemblyPartNumber).KeyProperty(x => x.PartNumber);
this.Map(x => x.CurrentDueDate);
this.Map(x => x.OrderLine);
this.Map(x => x.OrderNumber);
this.Map(x => x.PartDescription);
this.Map(x => x.ProductCode);
this.Map(x => x.Revision);
}
当我尝试创建会话工厂时,此映射会导致错误: 无法编译映射文档:(XmlDocument)
我尝试删除 CompositeId 映射并将其替换为:
this.Id(x => x.AssemblyPartNumber).GeneratedBy.Assigned();
错误随该映射消失,但我无法真正使用它,因为 AssemblyPartNumber 不是唯一的。
是否有不同的方法来映射到具有复合主键的表?
谢谢,
马修·麦克法兰
I am trying to use CompositeId to map to a legacy system. The source database has a composite primary key so I can't use the normal this.Id mapping.
Here is my attempt to map it:
public PriorityListPartMap()
{
this.Schema("EngSchedule");
this.Table("vPriorityListPart");
this.CompositeId().KeyProperty(x => x.AssemblyPartNumber).KeyProperty(x => x.PartNumber);
this.Map(x => x.CurrentDueDate);
this.Map(x => x.OrderLine);
this.Map(x => x.OrderNumber);
this.Map(x => x.PartDescription);
this.Map(x => x.ProductCode);
this.Map(x => x.Revision);
}
When I try to create the session factory this mapping causes the error:
Could not compile the mapping document: (XmlDocument)
I tried removing the CompositeId mapping and replaced it with:
this.Id(x => x.AssemblyPartNumber).GeneratedBy.Assigned();
The error goes away with that mapping but I can't really use that since the AssemblyPartNumber is not unique.
Is there a different way to map to a table with a composite primary key?
Thanks,
Matthew MacFarland
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“无法编译映射文档:(XmlDocument)”的内部异常是什么?我的理论是“composite-id 类必须重写 Equals(): YOURNAMESPACE.PriorityListPart”。
对于需要复合 ID 的实体,对象本身用作键。为了让“相同”的对象被识别为如此,您需要重写 Equals 和 GetHashCode 方法。
实体的 Equals 方法示例如下:
实体的 GetHashCode 方法示例如下:
这也意味着,如果您想要检索对象,则不能使用单个键来执行此操作。要正确检索特定对象及其复合键组件,您使用的键实际上是该对象的实例,并将复合键组件设置为您希望检索的实体。
这就是为什么必须重写 Equals() 方法,以便 NHibernate 能够根据您在 Equals 方法中指定的内容确定您实际尝试检索的对象。
What is the inner exception for "Could not compile the mapping document: (XmlDocument)"? My theory is it will be "composite-id class must override Equals(): YOURNAMESPACE.PriorityListPart".
For entities requiring composite-ids, the object itself is used as the key. In order for objects that are 'the same' to be recognized as so, you need to override the Equals and GetHashCode methods.
An example Equals method for your entity would be something like this:
An example GetHashCode method for your entity would be something like this:
This also means that if you want to retrieve an object, you cannot have a single key to do it with. To properly retrieve a specific object with its composite key components, the key you use is actually an instance of the object with the composite key components set to the entity you wish to retrieve.
This is why the Equals() method must be overridden, so that NHibernate has the ability to determine which object you are actually trying to retrieve, based on what you specify in the Equals method.