Reputation: 559
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();
}
}
Upvotes: 1
Views: 1119
Reputation: 22424
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.
Upvotes: 2