Manual identifier setting

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

Using ASPNET, I create a user account and return a GUID. I am trying to create related records in my tables with this GUID as the primary identifier.

The problem I am facing is that when I manually install User.ID NHibernate tries to update rather than paste. I see this with NHibernate Profiler before it launches "Unexpected row count: 0; Expected: 1".

My UsersMap for the Users table looks like this:

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.

+6
fluent-nhibernate- nhibernate-mapping
source share
3 answers

You need to indicate that your identifier will be assigned.

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

This will allow you to specify a value if NHibernate is trying to upgrade.

+16
source share

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

+1
source share

As an added value to James's answer, I used:

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

Note that the default value is the default value for your identifier type
like 0L in me, because I use long for my Id

0
source share

All Articles