Mapping Composite keys in Fluent NHibernate

前端 未结 4 1592
清歌不尽
清歌不尽 2020-12-14 05:39

I am new to fluent NHibernate. Now I face one problem with mapping composite keys. Can anyone point out the URL or sample please?

4条回答
  •  没有蜡笔的小新
    2020-12-14 06:29

    Another thing to note is that you must override the Equals and GetHashCode methods for an entity using a CompositeId. Given the accepted answers mapping file, your entity would look like this.

    public class Entity
    {
       public virtual int Something {get; set;}
       public virtual AnotherEntity SomethingElse {get; set;}
    
    
       public override bool Equals(object obj)
        {
            var other = obj as Entity;
    
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.SomethingElse == SomethingElse && other.Something == Something;
        }
    
        public override int GetHashCode()
        {
            unchecked
            {
                return (SomethingElse.GetHashCode()*397) ^ Something;
            }
        }
    
    }
    

提交回复
热议问题