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

发布于 2024-12-12 02:57:32 字数 841 浏览 1 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏璃陌 2024-12-19 02:57:32

“无法编译映射文档:(XmlDocument)”的内部异常是什么?我的理论是“composite-id 类必须重写 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 方法中指定的内容确定您实际尝试检索的对象。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文