CompositeId原因无法编译映射文档错误 [英] CompositeId causes Could not compile the mapping document error

查看:197
本文介绍了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);

$ / code>

当我尝试创建会话工厂时,这个映射导致错误:
无法编译映射文档:(XmlDocument)

我尝试删除CompositeId映射,并将其替换为:

  this.Id(x => x.AssemblyPartNumber).GeneratedBy.Assigned(); 

该映射错误消失了,但我不能真正使用它,因为AssemblyPartNumber不是唯一的。

是否有不同的方式映射到一个组合主键的表?



谢谢,



马修·麦克法兰

解决方案

编译映射文件:(XmlDocument)?我的理论是composite-id类必须重写Equals():YOURNAMESPACE.PriorityListPart。

对于需要composite-id的实体,对象本身被用作钥匙。为了使相同的对象被识别,你需要重写Equals和GetHashCode方法。

您实体的Equals方法的示例如下所示:

  public override bool Equals(object obj)
{
var other = obj as PriorityListPart;

if(ReferenceEquals(null,other))return false;
if(ReferenceEquals(this,other))返回true;

返回this.AssemblyPartNumber == other.AssemblyPartNumber&&
this.PartNumber == other.PartNumber;



$ b

你实体的一个GetHashCode方法就是这样的:



$ $ $ $




$ ().GetHashCode();
hash =(hash * 31)^ AssemblyPartNumber.GetHashCode();
hash =(hash * 31)^ PartNumber.GetHashCode();

返回散列;






$ b

这也意味着如果你想检索一个对象,你不能有一个单一的关键。要使用复合键组件正确检索特定对象,您使用的键实际上是对象的一个​​实例,将复合键组件设置为您希望检索的实体。

这就是Equals()方法必须被覆盖的原因,所以NHibernate能够根据你指定的内容来确定你实际上试图检索哪个对象Equals方法。

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

解决方案

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:

public override bool Equals(object obj)
{
    var other = obj as PriorityListPart;

    if (ReferenceEquals(null, other)) return false;
    if (ReferenceEquals(this, other)) return true;

    return this.AssemblyPartNumber == other.AssemblyPartNumber &&
        this.PartNumber == other.PartNumber;
}

An example GetHashCode method for your entity would be something like this:

public override int GetHashCode()
{
    unchecked
    {
        int hash = GetType().GetHashCode();
        hash = (hash * 31) ^ AssemblyPartNumber.GetHashCode();
        hash = (hash * 31) ^ PartNumber.GetHashCode();

        return hash;
    }
}

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.

这篇关于CompositeId原因无法编译映射文档错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆