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;
[DomainSi开发者_StackOverflow中文版gnature]
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; }
}
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);
精彩评论