CompositeId causes Could not compile the mapping document error

后端 未结 1 1565
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 06:33

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 att

相关标签:
1条回答
  • 2021-01-12 07:08

    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.

    0 讨论(0)
提交回复
热议问题