0

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).

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

3 Comments

Doesn't cascade="none" provide the same behavior?
I guess cascade prevents the Resource object to be saved but doesn't disable the save of they column itself. Thanks.
@Serge Cascading is a completely different concept. The insert and update attributes just say that NHibernate ignores that property for insert and update statements. Cascading in contrast is when the referenced entity should or shouldn't change. So cascade="none" just says that NHibernate shouldn't insert/delete AbstractHumanResource objects if your ADUser objects are inserted/deleted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.