Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manually setting the ID

I have a Users table where the "ID" field is a GUID field.

Using ASPNET I am creating a user account and getting the GUID back. I am trying to create associated records in my tables with that GUID as the main ID.

The problem I am running into is that when I manually set Users.ID NHibernate tries to do an Update, not an Insert. I see this with the NHibernate Profiler before it bombs out with "Unexpected row count: 0; Expected: 1".

My UsersMap for the table Users looks like:

public class UsersMap : ClassMap<Users>
{
    public UsersMap()
    {
        Id(x => x.ID, "ID"); //GUID

        Map(x => x.Name, "Name"); //string
        Map(x => x.PhoneNumber, "PhoneNumber"); //string
        Map(x => x.FaxNumber, "FaxNumber"); //string
        Map(x => x.EmailAddress, "EmailAddress"); //string

        HasMany<UsersAddressBook>(x => x.usersAddressBook).KeyColumn("ID");
    }
}

Any ideas? Thanks in advance.

like image 851
Michael D. Kirkpatrick Avatar asked Jun 03 '10 22:06

Michael D. Kirkpatrick


2 Answers

You need to specify that your id will be assigned.

Id(x => x.ID)
  .GeneratedBy.Assigned();

This will allow you to specify the value, without NHibernate trying to perform an update.

like image 59
James Gregory Avatar answered Nov 13 '22 05:11

James Gregory


ISession.Save has an overload that allows you to specify identifier. Try using it, and don't set your id property manually.

like image 40
maciejkow Avatar answered Nov 13 '22 04:11

maciejkow