Reputation: 6702
I'm getting an error "Invalid index... for this SqlParameterCollection" when I try to save my object with NHibernate. I've read a lot of answers about that problems but they either were about Fluent NHB or didn't seems to apply to my problem (I could be wrong about that).
Here are my hbm and my class :
<class name="MyWebSite.Model.ADUser" table="AD_USER">
<id name="Id" column="ID">
<generator class="native"/>
</id>
<property name="Login" column="LOGIN"/>
<property name="Hidden" column="HIDDEN"/>
<many-to-one name="Resource" column="LOGIN" property-ref="Login" cascade="none" />
</class>
[DataContract()]
public class ADUser : Entity.AbstractPersistentObject
{
[DataMember(EmitDefaultValue = false)]
public virtual string Login { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual bool Hidden { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual AbstractHumanResource Resource { get; set; }
}
AbstractPersistentObject is not a class I've defined myself, and I found not hbm file referencing it so I guess it's not bound to any mapping. It has the definition for the "ID" propery (I'm not even using the other ones).
[DataContract()]
public abstract class AbstractPersistentObject
{
[DataMember(EmitDefaultValue = false)]
public virtual int? Id { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual DateTime DateCreated { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual DateTime? DateUpdated { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual DateTime? DateDeleted { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual string CreatedBy { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual string UpdatedBy { get; set; }
[DataMember(EmitDefaultValue = false)]
public virtual string DeletedBy { get; set; }
public override int GetHashCode()
{
return !Id.HasValue ? base.GetHashCode() : string.Concat(this.GetType().FullName, Id.Value).GetHashCode();
}
public override bool Equals(object obj)
{
return obj == null ? false : obj.GetHashCode() == this.GetHashCode();
}
public override string ToString()
{
return this.GetType() + "#" + this.Id;
}
}
(I don't see problems with this inheritance)
When I get a list of data, it works fine. It's only when I try to save it that it throws an exception (I didn't try deletion yet).
Upvotes: 0
Views: 479
Reputation: 6876
You can't map two properties to the same column. How should NHibernate decide which value to use if they differ? If you really need the plain value of the many-to-one relation column as a seperate property too then map it with insert="false" update="false"
.
Upvotes: 1