1

I've read many posts here about exception mentioned in the title. Typically this exception means that somewhere I mapped to fields to one entity. I spent pretty much time looking at my mappings, but still can't find what's wrong. Could you please help me with understanding what've been done wrong here:

public class CustomerSegment : ModelEntity, IEntityDescriptor
{
    public const string Standart = "Standard";

    public virtual string Name { get; set; }
    public virtual NetworkNHMapped Network { get; set; }

    public virtual string GetDescriptor()
    {
        return Name;
    }
}

public class CustomerSegmentMap : ClassMap<CustomerSegment>
{
    public CustomerSegmentMap()
    {
        Table("NetworkProperty");
        Id(x => x.Id).Column("NetworkPropertyId");
        Map(x => x.Name).Column("PropertyName");
        References(x => x.Network).Column("NetworkId");
    }
}

}

The exception occurs when I'm trying get all CustomerSegment entities from DB.

The code of other entities:

public class NetworkNHMapped : ModelEntity
{
    [StringLength(50)]
    public virtual string Name { get; set; }
    public virtual int NetworkOwnerId { get; set; }
    public virtual int NetworkTypeId { get; set; }

    public virtual RepairShop.NetworkType NetworkType { get { return (RepairShop.NetworkType)NetworkTypeId; } }
}

public class NetworkNewMap : ClassMap<NetworkNHMapped>
{
    public NetworkNewMap()
    {
        Table("Network");
        Id(x => x.Id, "NetworkId");
        Map(x => x.Name, "NetworkName");
        Map(x => x.NetworkOwnerId, "NetworkOwnerId");
        Map(x => x.NetworkTypeId, "NetworkType");
    }
}

And base ModelEntity:

public virtual int Id { get; set; }

    public override int GetHashCode()
    {
        if (!IsPersisted())
        {
            return base.GetHashCode();
        }

        unchecked
        {
            int result = GetObjectRealType(this).GetHashCode();
            result = 42 * result + Id.GetHashCode();
            return result;
        }
    }

    public override bool Equals(object other)
    {
        if (ReferenceEquals(this, other))
        {
            return true;
        }

        if ((other == null) || !(other is ModelEntity))
        {
            return false;
        }

        var thisType = GetObjectRealType(this);
        var otherType = GetObjectRealType(other);

        if (thisType != otherType)
            return false;

        if (Id.Equals(default(long)) && (other as ModelEntity).Id.Equals(default(long)))
        {
            return base.Equals(other);
        }
        return Id == (other as ModelEntity).Id;
    }

    public static bool operator ==(ModelEntity entity1, ModelEntity entity2)
    {
        var obj1 = (object)entity1;
        if (obj1 == null && ((object)entity2) == null)
            return true;

        return obj1 != null && entity1.Equals(entity2);
    }

    public static bool operator !=(ModelEntity entity1, ModelEntity entity2)
    {
        return !(entity1 == entity2);
    }

    public virtual bool IsPersisted()
    {
        return Id > 0;
    }

    protected static Type GetObjectRealType(object obj)
    {
        return (obj is INHibernateProxy) ? NHibernateUtil.GetClass(obj) : obj.GetType();
    }

}
Community
  • 1
  • 1
valerii.sverdlik
  • 559
  • 4
  • 18
  • You may also post code for ModelEntity (is it mapped ?) as well as NetworkNHMapped (and its map) – jbl Dec 04 '13 at 13:14

1 Answers1

2

The first thing I would do is to look at the XML files that are being generated, this shows if you have duplicate mappings to the same property.

If you are using NH and ModelMapper then simply make a call to WriteAllXmlMapping e.g.:-

var mapper = new ModelMapper();
mapper.AddMappings(typeof(CmsMeta).Assembly.GetTypes());

//This will write all the XML into the bin/mappings folder
mapper.CompileMappingForEachExplicitlyAddedEntity().WriteAllXmlMapping();

If you are using Fluent NHibernate then look at this blog post to export your XML.

After you have generated the XML I bet you will find duplicate mappings! Otherwise see this blog post, it is possible if you are exposing a foreign-key as well as using a many-to-one ... to the related entity in the mapping file. If this is the case add insert="false" and update="false" to the foreign key property and run again.

Either way generate the XML and take a look, if you can't see it post the relevant XML files to your question and we will take a look.

Rippo
  • 22,117
  • 14
  • 78
  • 117
  • That was I great idea, but I still did not find an issue. I got 52 xml files and have no idea how to find the one problemmatic one. Nevertheless I have four suspicious mappings and would extremely thankful if you could review them, or share a strategy how to find an issue: https://www.dropbox.com/s/gylr07flj5ildfs/suspicious.zip – valerii.sverdlik Dec 04 '13 at 16:35
  • It looks like experimentally I found an issue. I have a mapping - it's a one-to-many connection between tables RepairShop and RepairShopWeekDayProperty. When I leave not null only in RepairShopWeekDayProperty table mapping everything works fine – valerii.sverdlik Dec 04 '13 at 17:53
  • Can you add to GIST `RepairShopWeekDayProperty.hmb.xml` ? BTW can't see any dups in mappings NOR where you have foreign key exposed. – Rippo Dec 04 '13 at 17:58
  • aha sorry in `RepairShopWeekDayProperty.hmb.xml` remove ` – Rippo Dec 04 '13 at 18:04