These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Working with identity column in MS Sql Express 2005
PostPosted: Sun Oct 08, 2006 11:09 pm 
Newbie

Joined: Fri Sep 29, 2006 11:13 pm
Posts: 10
I have a user_id table that has a user_id column as an identity column. I have mapped the table like this:

Code:
  <class name="Lifecloud.Core.Domain.User, Lifecloud.Core" table="User_ID">
    <id name="ID" type="int" column="User_ID" unsaved-value="0">
      <generator class="native" />
    </id>

    <property name="FirstName" column="FirstName" />
    <property name="MidInitial" column="MidInitial" />
    <property name="LastName" column="LastName" />
    <property name="EmailAddress" column="EmailAddress" />
    <property name="Status" column="Status" />
    <property name="LogonID" column="LogonID" />
    <property name="Gender" column="Gender" />
    <property name="Age" column="Age" />
    <property name="DateCreated" column="DateCreated" />
    <property name="DateModified" column="DateModified" />
</class.


My constructor is this:

Code:
        public User(string logonid) {
            LogonID = logonid;
        }


My aspx page has an add button that does this:

Code:
    protected void btnAdd_OnClick(object sender, EventArgs e) {
        User newUser = new User(txtLogonID.Text);
        newUser.FirstName = txtFirstName.Text;
        newUser.MidInitial = txtMidInitial.Text;
        newUser.LastName = txtLastName.Text;
        newUser.EmailAddress = txtEmailAddress.Text;
        newUser.Gender = txtGender.Text;
        newUser.Age = txtAge.Text;
        newUser.DateCreated = DateTime.Now;

        IDaoFactory daoFactory = new NHibernateDaoFactory();
        IUserDao userDao = daoFactory.GetUserDao();

        if (!IsDuplicateOfExisting(newUser, userDao)) {
            userDao.Save(newUser);
            Response.Redirect("ViewCustomers.aspx?action=added");
        }
        else {
            lblMessage.Text = "<span style=\"color:red\">The Logon ID you provided is already in use.</span>  Please change the ID and try again.";
        }
    }


Is there a way to find out what data NHibernate is trying to send to the database because the add button is gnerating the following error:

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


I have looked up the error in various forums and I realize that the command is trying to force something into my identity column but I don't know what it is. I want the MS SQL database to assign the id.

Thank you
Susan

_________________
Thank you,
Susan


Top
 Profile  
 
 Post subject: Re: Working with identity column in MS Sql Express 2005
PostPosted: Mon Oct 09, 2006 3:10 am 
Beginner
Beginner

Joined: Wed Aug 03, 2005 8:06 am
Posts: 40
Location: Netherlands
Hi,

This all looks pretty standard. The only thing that strikes me as a bit strange is that both your table and column are named 'User_ID'. Maybe NHibernate gets confused here somehow?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 26, 2006 4:57 pm 
Newbie

Joined: Thu Oct 26, 2006 4:49 pm
Posts: 8
That's the point:

<id name="ID" type="int" column="User_ID" unsaved-value="0">
<generator class="native" />
</id>

Try this:

<id name="ID" type="int" column="User_ID" unsaved-value="0">
<generator class="identity" />
</id>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.