CompositeId导致无法编译映射文档错误

时间:2011-10-27 12:31:39

标签: fluent-nhibernate fluent-nhibernate-mapping

我正在尝试使用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不是唯一的。

是否有不同的方法映射到具有复合主键的表?

谢谢,

Matthew MacFarland

1 个答案:

答案 0 :(得分:27)

“无法编译映射文档:(XmlDocument)”的内部异常是什么?我的理论是它将是“composite-id class必须覆盖Equals():YOURNAMESPACE.PriorityListPart”。

对于需要复合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)) return true;

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

您实体的示例GetHashCode方法如下:

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

        return hash;
    }
}

这也意味着如果你想要检索一个对象,你就不能用一个密钥来完成它。要使用其复合键组件正确检索特定对象,您使用的键实际上是对象的实例,其中复合键组件设置为您要检索的实体。

这就是必须重写Equals()方法的原因,以便NHibernate能够根据您在Equals方法中指定的内容来确定您实际尝试检索的对象。

相关问题