I have a bunch of C# classes which are inheriting a number of properties from an abstract base class. All of the types are mapped to a database model using Fluent NHibernate, and all of the property definitions use automatic getters and setters (the standard "get; set;" syntax). I recently found a need to provide a specific implementation for one of the accessor methods for a base type property on one of my derived types. So I created an explicit backing field for the base class property:
public abstract class BaseEntity : IBaseEntity
{
protected bool active_field;
...
public virtual bool active { get { return active_field; } set { active_field = value; } }
...
}
And then defined the specific getter logic for the "active" property in the derived type definition:
public override bool active
{
get { return active_field && (this.Expiration == null || this.Expiration < DateTime.Now); }
set { active_field = value; }
}
When I fire up the project, though, NHibernate throws an exception:
An exception of type 'System.InvalidOperationException' occurred in FluentNHibernate.dll but was not handled in user code. Additional information: Tried to add property 'active' when already added.
I'm guessing this has something to do with NHibernate needing to provide it's own override for the property definitions (hence why it requires the properties be declared virtual in the first place), but I'm not all that NHibernate savvy. That being the case, I'm happy to provide any other details, but don't really know what else is relevant. Is there an obvious reason why this can't work? If so, is there a simple workaround?
active
non-virtual wrapper to another non-public virtual property? WillNHibernate
fill non public property? - T.S.ClassMap<T>
or use Automapping? Also the override is flawed because when persisting, the active value can change even if active_field value did not change. better differentiate betweenIsActivated
/IsEnabled
(persisted) andIsActive
- Firo