views:

28

answers:

2

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.

A: 

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

maciejkow
+1  A: 

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.

James Gregory
This appears to be the best method. It also works perfectly.Thanks for your help.
Michael D. Kirkpatrick

related questions