Pun*_*eet 32 .net enums fluent-nhibernate
我正在关注http://wiki.fluentnhibernate.org/Getting_started教程,用Fluent NHibernate创建我的第一个NHibernate项目
我有2张桌子
1)帐户与字段
Id
AccountHolderName
AccountTypeId
Run Code Online (Sandbox Code Playgroud)
2)带有字段的AccountType
Id
AccountTypeName
Run Code Online (Sandbox Code Playgroud)
现在,帐户类型可以是Savings或Current所以表AccountTypes存储2行1 - Savings 2 - Current
对于AccoutType表,我已经定义了枚举
public enum AccountType {
Savings=1,
Current=2
}
Run Code Online (Sandbox Code Playgroud)
对于Account表,我定义了实体类
public class Account {
public virtual int Id {get; private set;}
public virtual string AccountHolderName {get; set;}
public virtual string AccountType {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
流畅的nhibernate映射是:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType);
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行解决方案,它提供了一个异常 - 的InnerException = {"(XmlDocument的)(2,4):XML验证错误:元素'类’在命名空间'瓮:NHibernate的映射-2.2’具有不完整的内容.期望的可能元素列表:'命名空间'中的元,子选择,缓存,同步,注释,tuplizer,id,composite-id'...
我想那是因为我还没有为AccountType指定任何映射.
问题是:
谢谢!
Dan*_*ann 63
以下显然不再有效/sf/answers/35232921/
这样做怎么样:
public AgencyMap() {
Id(o => o.Id);
Map(o => o.AccountHolderName);
Map(o => o.AccountType).CustomType<AccountType>();
}
Run Code Online (Sandbox Code Playgroud)
自定义类型处理一切:)
mhe*_*xon 37
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);
}
Run Code Online (Sandbox Code Playgroud)
如果要覆盖需要为其提供约定,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
}
Run Code Online (Sandbox Code Playgroud)
几乎忘了您还需要将常规添加到您的流畅配置中.您可以在添加映射的同一位置执行此操作:
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BillingRecordMap>()
.Conventions.AddFromAssemblyOf<EnumConvention>()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22660 次 |
| 最近记录: |