我正在关注http://wiki.fluentnhibernate.org/Getting_started教程,用Fluent NHibernate创建我的第一个NHibernate项目
我有2张桌子
1)带字段的帐户
Id
AccountHolderName
AccountTypeId
2)包含字段的帐户类型
Id
AccountTypeName
现在,帐户类型可以是Savings或Current 因此表AccountTypes存储2行 1 - 节省 2 - 当前
对于AccoutType表,我定义了枚举
public enum AccountType {
Savings=1,
Current=2
}
对于Account表,我定义了实体类
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual string AccountType {get; set;}
}
流畅的nhibernate映射是:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
当我尝试运行解决方案时,它给出了一个异常 - InnerException = {“(XmlDocument)(2,4):XML验证错误:命名空间'urn:nhibernate-mapping-2.2'中的元素'class'具有不完整的内容。预期可能的元素列表:名称空间中的'meta,subselect,cache,synchronize,comment,tuplizer,id,composite-id'...
我想这是因为我没有为AccountType指定任何映射。
问题是:
谢谢!
答案 0 :(得分:60)
以下显然不再有效https://stackoverflow.com/a/503327/189412
如何做到这一点:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType).CustomType<AccountType>();
}
自定义类型处理所有内容:)
答案 1 :(得分:36)
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual AccountType AccountType {get; set;}
}
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
如果要覆盖需要为其提供约定,Fluent NHibernate默认将枚举值保存为字符串。类似的东西:
public class EnumConvention :
IPropertyConvention,
IPropertyConventionAcceptance
{
#region IPropertyConvention Members
public void Apply(IPropertyInstance instance)
{
instance.CustomType(instance.Property.PropertyType);
}
#endregion
#region IPropertyConventionAcceptance Members
public void Accept(IAcceptanceCriteria<IPropertyInspector> criteria)
{
criteria.Expect(x => x.Property.PropertyType.IsEnum ||
(x.Property.PropertyType.IsGenericType &&
x.Property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) &&
x.Property.PropertyType.GetGenericArguments()[0].IsEnum)
);
}
#endregion
}
几乎忘了您还需要将常规添加到您的流畅配置中。您可以在添加映射的同一位置执行此操作:
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BillingRecordMap>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
答案 2 :(得分:1)
一个很好的方法是实现接口IUserType和Create一个CustomType以及写入和读取的规则,这是布尔的一个例子:
public class CharToBoolean : IUserType
{
public SqlType[] SqlTypes => new[] { NHibernateUtil.String.SqlType };
public Type ReturnedType => typeof(bool);
public bool IsMutable =>true;
public object Assemble(object cached, object owner)
{
return (cached);
}
public object DeepCopy(object value)
{
return (value);
}
public object Disassemble(object value)
{
return (value);
}
public new bool Equals(object x, object y)
{
if (ReferenceEquals(x, y)) return true;
var firstObject = x as string;
var secondObject = y as string;
if (string.IsNullOrEmpty(firstObject) || string.IsNullOrEmpty(secondObject)) return false;
if (firstObject == secondObject) return true;
return false;
}
public int GetHashCode(object x)
{
return ((x != null) ? x.GetHashCode() : 0);
}
public object NullSafeGet(IDataReader rs, string[] names, object owner)
{
var obj = NHibernateUtil.String.NullSafeGet(rs, names[0]);
if (obj == null) return null;
var value = (string)obj;
return value.ToBoolean();
}
public void NullSafeSet(IDbCommand cmd, object value, int index)
{
if(value != null)
{
if ((bool)value)
{
((IDataParameter)cmd.Parameters[index]).Value = "S";
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = "N";
}
}
else
{
((IDataParameter)cmd.Parameters[index]).Value = DBNull.Value;
}
}
public object Replace(object original, object target, object owner)
{
return original;
}
}
}
映射:
this.Map(x => x.DominioGenerico).Column("fldominiogen").CustomType<CharToBoolean>();
这是一个示例,但您可以使用其他类型执行此操作。