1
votes

I'm a novice with NHibernate and am encountering a weird behavior (using NHibernate 2.1.2.4000 FluentNHibernate version 1.1.0.685). Somehow the id which is a Guid is getting reset to empty after you first access the Item object. Is there some side-effect that happens when accessing a related object as in line 2?

1. System.Diagnostics.Debug.WriteLine(widgetQueue.Item.Id);
2. var ItemStageId = widgetQueue.Item.CurrentStage.Id.ToString();
3. System.Diagnostics.Debug.WriteLine(widgetQueue.Item.Id);

Output Window:   
113a6af2-3fe2-49c2-9276-9ec30081a811    
00000000-0000-0000-0000-000000000000

Update:

I changed the Id field from new virtual:

public class Item : EntityWithTypedId<Guid>
{
    private Guid id;

    [DomainSignature]
    public new virtual Guid Id
    {
        get { return id; }
        protected set { id = value; }
    }
     ....
    public Item() {
         Id = Guid.Empty;
         ....
    }

    public Item(Guid id)
        : base()
    {
        Id = id;
    }
}

to override:

public class Item : EntityWithTypedId<Guid>
{
    private Guid id;

    [DomainSignature]
    public override Guid Id
    {
        get { return id; }
        protected set { id = value; }
    }
     ....
}

I was expecting a runtime error since I read properties need to be virtual for the lazy loading to work. Any clue what's happening?

Update 2: I noticed that we declared an instance variable to support the Id property. So, I removed it and accessed the base class' Id. This works and makes more sense, still haven't found the reasoning why the previous attempts failed.

public class Item : EntityWithTypedId { //private Guid id;

    [DomainSignature]
    public new virtual Guid Id
    {
        get { return base.Id; }
        protected set { base.Id = value; }
    }
1
Can you post code for your widget queue and item classes? Any relevant mappings would be helpful as well. - AlexCuse
I tinkered around a bit more and think I made some progress. I'm not sure how to show you the mappings. I'm using the Sharp Architecure framework and haven't learned how it generates models. - PPC-Coder
I'm not familiar with the latest versions of Sharp Arch., but two things strike me as strange with the code: overriding (or using new) the ID property, and accepting the ID using a constructor. This seems like it could lead to problems. - cbp
Can you please elaborate on the proper way to use ID properties? - PPC-Coder

1 Answers

1
votes

EntityWithTypedId does already define an Id-property, you dont have to declare that yourself.

for example in fluent mappings you can safely use the id property with:

Id(x => x.Id).GeneratedBy.GuidComb().UnsavedValue(Guid.Empty);