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

Mat*_*and 8 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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

this.Id(x => x.AssemblyPartNumber).GeneratedBy.Assigned();
Run Code Online (Sandbox Code Playgroud)

错误随着映射而消失,但我无法真正使用它,因为AssemblyPartNumber不是唯一的.

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

谢谢,

马修麦克法兰

doc*_*tan 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;
}
Run Code Online (Sandbox Code Playgroud)

您的实体的示例GetHashCode方法将是这样的:

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

        return hash;
    }
}
Run Code Online (Sandbox Code Playgroud)

这也意味着如果要检索对象,则不能使用单个键来执行此操作.要使用其复合键组件正确检索特定对象,您使用的键实际上是对象的实例,其中复合键组件设置为您要检索的实体.

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