Free class base class NHibernate fleet

I have the following base class.

public abstract class BaseEntity
{
    public virtual long Id { get; set; }
    public virtual DateTime CreateDate { get; set; }
    public virtual DateTime? UpdateDate { get; set; }
    public virtual bool IsDeleted { get; set; }
    public virtual string CreatedBy { get; set; }
}

And this is the base class for all objects.

For instance,

public class Task : BaseEntity
{
    public virtual string Name {get;set;}
}

I have an initializer,

public class Initializer
{
    public static AutoPersistenceModel MapEntities()
    {
        var p = AutoMap
            .AssemblyOf<User>(new MyDefaultAutomappingConfiguration())
             .IgnoreBase<BaseEntity>()
            .UseOverridesFromAssemblyOf<Initializer>()
            .Override<BaseEntity>(map =>
            {
                map.Map(b => b.CreateDate).Not.Nullable().Not.Update();
                map.Map(b => b.CreatedBy).Not.Nullable().Length(100);
                map.Map(b => b.ModifiedBy).Length(100);
                map.Map(b => b.DeletedBy).Length(100);
                map.Map(b => b.IsDeleted).Not.Nullable().Default("0");
            });
        return p;
    }
}

And, of course, in the database - CreateDate - it is datetime, null and CreatedBy - nvarchar (255).

How can I configure this AutoMapper to get these mappings from the base class to all child classes?

+3
source share
1 answer

You cannot override the mapping for the base class, as this mapping is not actually created. If you want to continue using Automapper, you can create an agreement

public class BaseEntityPropertiesConvention : IPropertyConvention
{
    public void Apply(IPropertyInstance instance)
    {
        if (instance.EntityType.IsSubclassOf(typeof(BaseEntity)))
        {
            switch instance.Name
            {
                case "CreatedDate":
                    instance.Not.Nullable().Not.Update();
                    break;
                case "CreatedBy":
                    instance.Not.Nullable().Length(100);
                    break;
                //etc...
            }
        }
    }
}
+3
source

All Articles