Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Fluent NHibernate自动映射基类重写_C#_.net_Nhibernate_Fluent Nhibernate_Automapping - Fatal编程技术网

C# Fluent NHibernate自动映射基类重写

C# Fluent NHibernate自动映射基类重写,c#,.net,nhibernate,fluent-nhibernate,automapping,C#,.net,Nhibernate,Fluent Nhibernate,Automapping,我有下面的基类 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

我有下面的基类

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; }
}
它是所有实体的基类

比如说,

public class Task : BaseEntity
{
    public virtual string Name {get;set;}
}
我有一个初始值设定项

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;
    }
}
公共类初始值设定项
{
公共静态AutoPersistenceModel MapEntities()
{
var p=自动映射
.AssemblyOf(新的MyDefaultAutomappingConfiguration())
.IgnoreBase()
.UseOverridesFromAssemblyOf()的
.Override(映射=>
{
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”);
});
返回p;
}
}
当然,在数据库中,CreateDate是datetime,null,CreatedBy是nvarchar(255)


如何配置此自动映射程序以获取从基类到所有子类的映射?

您不能覆盖基类的映射,因为该映射实际上从未创建过。如果要继续使用自动映射,可以创建约定

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...
            }
        }
    }
}