0

I have issue with NHibernate it some times it doesn't update ID property

User Map Class

public class SfGuardUserMap : ClassMap<User> {

    public SfGuardUserMap() {
        Table("sf_guard_user");
        LazyLoad();
        Id(x => x.Id).GeneratedBy.Identity().Column("id");
        Map(x => x.FirstName).Column("first_name");
        Map(x => x.LastName).Column("last_name");
        Map(x => x.EmailAddress).Column("email_address").Not.Nullable();
        Map(x => x.Sex).Column("sex").Not.Nullable();
        Map(x => x.Lang).Column("lang");
        Map(x => x.City).Column("city");

Executable code

var user = new User
                       {
                           FirstName = usr.FirstName,
                           EmailAddress = usr.Email,
                           LastName = usr.LastName,
                           Sex = usr.Sex,
                           Birthdate = usr.Birthday,
                           IsActive = User.IS_ACTIVE_ACTIVE
                       };
        var t = (int)session.Save(user);
        session.Flush();

I had issues and user.Id was always 0. But when I restarted iis my issue gone.

I have a bit the same issue with other class. Is this here any property insert to database when I call Save?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Vova Bilyachat
  • 18,765
  • 4
  • 55
  • 80

1 Answers1

4

I'm not 100% sure what is the problem given your description, but I believe that I can give some pointers based on the code you've posted:

  1. you're using Identity ID generator; that means that you allow your database to generate the ids of entities as you insert them. This approach is not recommended. I would suggest using HiLo as your id generator. If you stick with identity, however, you should make sure that your db is configured accordingly.

  2. it's recommended to always use transactions instead of explicitly calling Flush.

if these suggestion don't help you, I suggest you detail some more on your issue- what are you seeing (are the objects saved in the db? is the id returned always 0?)

Community
  • 1
  • 1
J. Ed
  • 6,692
  • 4
  • 39
  • 55