1
votes

I seem to be having a random issue with nHibernate and inserting records.

Insert was working fine, then added a new table, and started receiving the error:

Cannot insert explicit value for identity column in table '' when IDENTITY_INSERT is set to OFF

The existing tables insert also now fails. The random part comes in that if I delete my files, and re-checkout, the insert then works fine.

nHibernate version is 3.1

The identity columns is being set via:

[NHibernate.Mapping.Attributes.Generator(Class = "native")]
[NHibernate.Mapping.Attributes.Id(Name = "id")]
public virtual IdT id
{
    get { return _id; }
    set { _id = value; }
}

I tried setting to "identity", but still an issue. Not sure if there is a caching problem, but worried it could affect functionality at a later stage.

1
could you elaborate on the new table you've added? how is it mapped? - J. Ed

1 Answers

4
votes

Was facing this error. I immediately realized that its an IDENTITY issue. But i had auto-increment set. So i checked my mapping file. I was quick to realize that:

    <id name="Id" access="property" column="`id`">
        <generator class="assigned" />
    </id>

has to be

    <id name="Id" access="property" column="`id`">
        <generator class="native" />
    </id>

NOTE: class="native"