nhibernate - 为所有域类添加带有接口(interface)的 IAutoMappingOverride

标签 nhibernate fluent-nhibernate nhibernate-mapping fluent-nhibernate-mapping

我有一个 IAuditable将我的类域定义为可审计的接口(interface)。

public interface IAuditable
{
    DateTime CreateAt { get; }
    IUser CreateBy { get; }
    DateTime? UpdateAt { get; }
    IUser UpdateBy { get; }
}

对于这些实现这个接口(interface)的类(有很多)来说,配置是一样的!所以我决定重写约定:
public class AudityConvention : IAutoMappingOverride<IAuditable>
{
    public void Override(AutoMapping<IAuditable> mapping)
    {
        mapping.Map(p => p.CreateAt).ReadOnly().Access.Property().Not.Nullable().Not.Update().Default("getDate()").Generated.Insert();
        mapping.References<Usuario>(p => p.CreateBy).Not.Nullable().Not.Update();

        mapping.Map(p => p.UpdateAt).ReadOnly().Access.Property().Default("getDate()").Not.Insert().Generated.Always();
        mapping.References<Usuario>(p => p.UpdateBy).Nullable().Not.Insert();
    }
}

并配置它
    _configuration = Fluently.Configure() // All config from app.config
        .Mappings(m =>
        {
            m.AutoMappings.Add(
                AutoMap.AssemblyOf<Usuario>()
                .UseOverridesFromAssemblyOf<AudityConvention>()
                .Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>())
            );

            m.FluentMappings
                .AddFromAssemblyOf<UsuarioMap>()
                .Conventions.AddFromAssemblyOf<EnumConvention>()
                                    ;
        })
        .BuildConfiguration();

SessionFactory = _configuration.BuildSessionFactory();

Session = SessionFactory.OpenSession();

var export = new SchemaExport(_configuration);
export.Drop(false, true); // drop and recreate the database (Just to make sure that the settings are being applied)
export.Execute(false, true, false); // Create de database 

有了这个应用程序配置
    <appSettings>
    <add key="FluentAssertions.TestFramework" value="mstest"/>
</appSettings>
    <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
        <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
        <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
        <property name="connection.connection_string_name">Data</property>
        <property name="show_sql">true</property>
    </session-factory>
</hibernate-configuration>

示例域类:
public class Entidade : IAuditable
{
    public virtual int Id { get; protected set; }
    [StringLength(255)]
    public virtual string Nome { get; set; }

    // Implements IAuditable
    public virtual DateTime CreateAt { get; protected set; }
    public virtual IUser CreateBy { get; set; }
    public virtual DateTime? UpdateAt { get; protected set; }
    public virtual IUser UpdateBy { get; set; }
}

并映射它:
public class EntidadeMap : ClassMap<Entidade>
{
    public EntidadeMap()
    {
        Id(p => p.Id);
        Map(p => p.Nome);

        Table("Entidades");
    }
}

结果:

enter image description here

问题

我究竟做错了什么?
如何为所有实现 IAuditable 的类创建约定设置是一样的!

下面的配置部分是后来添加的。根据我的阅读,仅由 AutoMappings 覆盖支持公约。
m.AutoMappings.Add(
    AutoMap.AssemblyOf<Usuario>()
    .UseOverridesFromAssemblyOf<AudityConvention>()
    .Conventions.Setup(c => c.AddFromAssemblyOf<EnumConvention>())
);

最佳答案

我觉得你运气不好。我不相信覆盖在接口(interface)上的工作。
你最好做这样的事情:

public class BaseAuditable : IAuditable
{
   ....
}

public class Entidade : BaseAuditable
{ ... }

您可以使用
  IAutoMappingOverride<BaseAuditable> instead of IAutoMappingOverride<IAuditable>

我希望我错了,但我试图让它在没有运气的接口(interface)上工作了很多次。

关于nhibernate - 为所有域类添加带有接口(interface)的 IAutoMappingOverride,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16640114/

相关文章:

NHIbernate 只读属性 - 流畅的映射

c# - 判断NHibernate实体是否有级联记录

c# - 如何使用 FK NHibernate 映射复合 PK

c# - FluentNhibernate - HasMany 与复合键的关系

c# - Fluent NHibernate 映射一个返回动态列集的存储过程

nhibernate - FluentNHibernate,从另一个表中获取 1 列

asp.net-mvc - ASP.NET MVC 中的 NHibernate session 管理

c# - 帮助 QueryOver 和 WhereExists

nhibernate - NHibernate Join Fetch(种类)

c# - Fluent Nhibernate List<string> 映射