除了使用 NH 的按代码映射之外,这与此问题几乎相同。
我真的需要虚拟属性,因为我还想使用 SchemaExport 为不同的 rdbms 创建数据库,而无需为每个 rdbms 创建/维护脚本。
也许有一位 MbC Guru 知道如何使用 MbC
更新:明显的简单代码
Property("dummyProperty", c =>
{
c.Column("legacyColumn");
c.Access(typeof(MyPropertyAccessor));
});
不工作
NHibernate.MappingException: Unable to instantiate mapping class (see InnerException): Test.MbC.GroupMap ---> System.Reflection.TargetInvocationException: Ein Aufrufziel hat einen Ausnahmefehler verursacht. ---> NHibernate.MappingException: Member not found. The member 'dummyProperty' does not exists in type Test.Data.Group
bei NHibernate.Mapping.ByCode.Impl.CustomizersImpl.PropertyContainerCustomizer`1.GetPropertyOrFieldMatchingNameOrThrow(String memberName)
bei NHibernate.Mapping.ByCode.Impl.CustomizersImpl.PropertyContainerCustomizer`1.RegisterNoVisiblePropertyMapping(String notVisiblePropertyOrFieldName, Action`1 mapping)
bei
...
这也不是,因为上帝知道为什么MbC 在内部通过反射检查该属性确实存在于 class 上。
var parameter = Expression.Parameter(typeof(T), "x");
Expression body = Expression.Property(parameter, new GetterPropertyInfo(typeof(T), defaultgetter));
body = Expression.Convert(body, typeof(object));
var lambda = Expression.Lambda<Func<T, object>>(body, parameter);
Property(lambda, m =>
{
m.Column(defaultgetter.PropertyName);
m.Access(propertyAccessorType);
});
甚至通过在 ClassMapping 中重写来禁用反射测试,RegisterProperty()
它在构建 hbm 时仍然会抛出抱怨:
System.ArgumentOutOfRangeException: Can't add a property of another graph
Parametername: property
bei NHibernate.Mapping.ByCode.Impl.AbstractBasePropertyContainerMapper.Proper
ty(MemberInfo property, Action`1 mapping)
bei NHibernate.Mapping.ByCode.ModelMapper.MapProperty(MemberInfo member, Prop
ertyPath propertyPath, IMinimalPlainPropertyContainerMapper propertiesContainer)
bei NHibernate.Mapping.ByCode.ModelMapper.MapProperties(Type propertiesContai
nerType, IEnumerable`1 propertiesToMap, IPropertyContainerMapper propertiesConta
iner, PropertyPath path)
bei NHibernate.Mapping.ByCode.ModelMapper.MapProperties(Type propertiesContai
nerType, IEnumerable`1 propertiesToMap, IPropertyContainerMapper propertiesConta
iner)
bei NHibernate.Mapping.ByCode.ModelMapper.MapRootClass(Type type, HbmMapping
mapping)
bei NHibernate.Mapping.ByCode.ModelMapper.CompileMappingFor(IEnumerable`1 typ
es)
按代码映射应该比FNH更灵活?在哪里?