0
votes

My database has a strong entity Polygon and a weak entity Vertex. I implement these entities and relation with entity and mapping classes below. but get this exception: An invalid or incomplete configuration was used while creating a SessionFactory. Check PotentialReasons collection, and InnerException for more detail.

  public class Polygon
    {
        public virtual int Id { set; get; }
        public virtual IList<Vertex> Vertices { set; get; }

    }

    public class Vertex
    {
        public virtual int Number { set; get; }
        public virtual int X { set; get; }
        public virtual int Y { set; get; }
        public virtual Polygon Polygon { set; get; }
    }


    public class PolygonMap : ClassMap<Polygon>
    {
        public PolygonMap()
        {
            Id(x => x.Id).GeneratedBy.Identity();
            HasMany(x => x.Vertices).KeyColumn("PolygonId").Inverse().Cascade.AllDeleteOrphan();
            Table("Polygon");
        }
    }

    public class VertexMap : ClassMap<Vertex>
    {
        public VertexMap()
        {
            CompositeId()
                .KeyProperty(x => x.Number, "Number")
                .KeyReference(x => x.Polygon, "PolygonId");
            Map(x => x.X);
            Map(x => x.Y);
            References(x => x.Polygon).Column("PolygonId");
            Table("Vertex");
        }
    }
1

1 Answers

0
votes

I found the solution

the weak entity class must override Equals and GetHashCode methods(inherited from Object class) like below:

 public class Vertex
    {
        public virtual int Number { set; get; }
        public virtual int X { set; get; }
        public virtual int Y { set; get; }
        public virtual Polygon Polygon { set; get; }
        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;

            Vertex V = (Vertex)obj;
            if (V == null)
                return false;

            if (Number == V.Number && Polygon.Id == V.Polygon.Id)
                return true;

            return false;
        }

        public override int GetHashCode()
        {
            return (Number + "|" + Polygon.Id).GetHashCode();
        }

    }

Regards...