我的数据库中包含varchar
个值,而NHibernate正在将type="string"
映射到nVarchar
。我知道我可以使用type="AnsiString"
而不是" string",但似乎能够在项目级别而不是列级别设置它会更有意义。
有没有办法告诉NHibernate在全球范围内使用string=AnsiString
?我没有使用Fluent nHibernate。
答案 0 :(得分:0)
如果你打算在系统范围内使用string = AnsiString,你必须在NHibernate约定级别修复它。
public class StringVarCharConvention : IPropertyConvention
{
public void Apply(IPropertyInstance instance)
{
if (instance.Property.PropertyType == typeof (string))
instance.CustomType("AnsiString");
}
}
然后您必须将此约定添加到自动映射配置
Fluently.Configure()
.Mappings( m =>
m.AutoMappings.Add( AutoMap.AssemblyOf<Foo>()
.Conventions.Add< StringVarCharConvention >()))
答案 1 :(得分:0)
使用代码映射(来自NH核心),您可以执行以下操作:
ModelMapper mapper = new ModelMapper();
// ...
var exportedTypes = assembly.GetTypes();
mapper.BeforeMapProperty += MapperOnBeforeMapProperty;
mapper.AddMappings(exportedTypes);
private void MapperOnBeforeMapProperty(
IModelInspector modelInspector,
PropertyPath member,
IPropertyMapper propertyMapper)
{
// add special handling for fields if you also
// map fields
PropertyInfo info = (PropertyInfo)member.LocalMember;
if (info.PropertyType == typeof(string)
{
propertyMapper.Type(NHibernateUtil.AnsiString, null);
}
}