Database 如何从NHibernate中的审核事件侦听器中获取数据库列名
我正在尝试用修改后的值审核/记录表名和字段名 我的DTO与数据库的名称不同:Database 如何从NHibernate中的审核事件侦听器中获取数据库列名,database,nhibernate,event-listener,Database,Nhibernate,Event Listener,我正在尝试用修改后的值审核/记录表名和字段名 我的DTO与数据库的名称不同: public class Emp { public virtual int EmployeeNo { get; set; } public virtual string FirstNames { get; set; } public virtual string Surname { get; set; } } 我的绘图程序如下: var mapper = new ModelMapper()
public class Emp
{
public virtual int EmployeeNo { get; set; }
public virtual string FirstNames { get; set; }
public virtual string Surname { get; set; }
}
我的绘图程序如下:
var mapper = new ModelMapper();
mapper.Class<Emp>(rc =>
{
rc.Table("EMP");
rc.Id(x => x.EmployeeNo, m => m.Column("EMPLOYEE_NO"));
rc.Property(x => x.FirstNames, map => map.Column("FIRST_NAMES"));
rc.Property(x => x.Surname, map => map.Column("SURNAME"));
});
var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
有人能告诉我一种获取数据库列名的方法吗?如果可以将它们映射到正在插入/更新的实际值
如您所见,我确实可以访问表名和主键字段。。。但是找不到其他属性。
这是我要求NHibernate获取所有信息的方式。在我的情况下,所有的设置,如列名,可以插入,可以更新。。。已经存在,并且可以/应该使用(即使仅用于文档)。因此,下面是经过简化但完全可以工作的代码片段 首先,让我们创建一些描述对象
MappingDescription
:,将所有设置移动到其中,并将其作为IList
返回
public interface IMappingDescription
{
string PropertyName { get; }
string ColumnName { get; }
bool CanInsert { get; }
bool CanUpdate { get; }
Type PropertyType { get; }
bool IsLocalDateTime { get; }
}
静态方法,用于任何映射的持久类:
public static IList<MappingDescription> ReadMappingInfo<TEntity>()
{
var entityType = typeof(TEntity);
var factory = ... // Get your ISessionFactory
var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;
if (persister == null)
{
throw new InvalidCastException("NHibernate ISessionFactory did not return 'AbstractEntityPersister' on GetClassMetadata()");
}
// the set of Properties
var propertyNameList = persister.PropertyNames;
// the result
var map = new List<MappingDescription>();
// here we inject the mapping for ID property (execting one column per ID)
map.Add(new MappingDescription
{
PropertyName = Constants.Common.ID,
ColumnName = persister.KeyColumnNames.First(),
PropertyInfo = entityType.GetProperty(Constants.Common.ID),
});
// here we get all the settings for remaining mapped properties
foreach (var propertyName in propertyNameList)
{
var index = persister.GetPropertyIndex(propertyName);
// a check how to distinguish if we do work in UTC or Local
var isLocal = persister.GetPropertyType(propertyName) is NHibernate.Type.LocalDateTimeType
|| persister.GetPropertyType(propertyName) is NHibernate.Type.TimestampType;
var description = new MappingDescription
{
PropertyName = propertyName,
ColumnName = persister.GetPropertyColumnNames(propertyName).First(),
PropertyInfo = entityType.GetProperty(propertyName),
CanInsert = persister.PropertyInsertability[index],
CanUpdate = persister.PropertyUpdateability[index]
|| persister.PropertyTypes[index].IsCollectionType, // <idbag> has Updatebility false
IsLocalDateTime = isLocal,
};
map.Add(description);
}
return map;
}
公共静态IList ReadMappingInfo()
{
var entityType=类型(TEntity);
var factory=…//获取您的ISessionFactory
var persister=factory.GetClassMetadata(entityType)作为AbstractEntityTyperMaster;
if(persister==null)
{
抛出新的InvalidCastException(“NHibernate ISessionFactory未在GetClassMetadata()上返回'AbstractEntityPersister'”;
}
//属性集
var propertyNameList=persister.PropertyNames;
//结果
var map=新列表();
//这里,我们为ID属性注入映射(每个ID执行一列)
map.Add(新映射说明)
{
PropertyName=Constants.Common.ID,
ColumnName=persister.KeyColumnNames.First(),
PropertyInfo=entityType.GetProperty(Constants.Common.ID),
});
//在这里,我们获得其余映射属性的所有设置
foreach(propertyNameList中的变量propertyName)
{
var index=persister.GetPropertyIndex(propertyName);
//检查如何区分我们是在UTC工作还是在本地工作
var isLocal=persister.GetPropertyType(propertyName)是NHibernate.Type.LocalDateTimeType
||GetPropertyType(propertyName)是NHibernate.Type.TimestampType;
var description=新映射描述
{
PropertyName=PropertyName,
ColumnName=persister.GetPropertyColumnNames(propertyName).First(),
PropertyInfo=entityType.GetProperty(propertyName),
CanInsert=persister.PropertyInsertability[index],
CanUpdate=persister.PropertyUpdateability[索引]
||persister.PropertyTypes[index].IsCollectionType,//具有可更新性false
IsLocalDateTime=isLocal,
};
地图。添加(说明);
}
返回图;
}
public static IList<MappingDescription> ReadMappingInfo<TEntity>()
{
var entityType = typeof(TEntity);
var factory = ... // Get your ISessionFactory
var persister = factory.GetClassMetadata(entityType) as AbstractEntityPersister;
if (persister == null)
{
throw new InvalidCastException("NHibernate ISessionFactory did not return 'AbstractEntityPersister' on GetClassMetadata()");
}
// the set of Properties
var propertyNameList = persister.PropertyNames;
// the result
var map = new List<MappingDescription>();
// here we inject the mapping for ID property (execting one column per ID)
map.Add(new MappingDescription
{
PropertyName = Constants.Common.ID,
ColumnName = persister.KeyColumnNames.First(),
PropertyInfo = entityType.GetProperty(Constants.Common.ID),
});
// here we get all the settings for remaining mapped properties
foreach (var propertyName in propertyNameList)
{
var index = persister.GetPropertyIndex(propertyName);
// a check how to distinguish if we do work in UTC or Local
var isLocal = persister.GetPropertyType(propertyName) is NHibernate.Type.LocalDateTimeType
|| persister.GetPropertyType(propertyName) is NHibernate.Type.TimestampType;
var description = new MappingDescription
{
PropertyName = propertyName,
ColumnName = persister.GetPropertyColumnNames(propertyName).First(),
PropertyInfo = entityType.GetProperty(propertyName),
CanInsert = persister.PropertyInsertability[index],
CanUpdate = persister.PropertyUpdateability[index]
|| persister.PropertyTypes[index].IsCollectionType, // <idbag> has Updatebility false
IsLocalDateTime = isLocal,
};
map.Add(description);
}
return map;
}