Fluent NHibernate 映射组件,一种已索引,一种未索引
在VerifyTheMapping 测试中遇到错误:
System.ApplicationException:对于属性“ANonindexedComponent” 预期类型为“Generic.TheDomain.NonindexedComponent” 'Generic.TheDomain.NonindexedComponent' 但得到了 '' 类型 'Generic.TheDomain.NonindexedComponent' 和 System.ApplicationException:对于属性“IndexedComponent”预期 类型为“Generic.TheDomain.IndexedComponent” 'Generic.TheDomain.IndexedComponent' 但得到了 '' 类型 'Generic.TheDomain.IndexedComponent'
:
namespace Generic.TheDomain
{
public class UseAIndexedComponent:DomainEntity<UseAIndexedComponent>
{
public virtual string NameTitle { get; private set; }
public virtual IndexedComponent IndexedComponent { get; private set; }
}
public class UseANonindexedComponent:DomainEntity<UseANonindexedComponent>
{
public virtual string TitleName { get; private set; }
public virtual NonindexedComponent
ANonindexedComponent { get; private set; }
}
//we want this one to have a unique index on A B C
public class IndexedComponent
{
public virtual string A { get; private set; }
public virtual string B { get; private set; }
public virtual string C { get; private set; }
public IndexedComponent(){}
}
//no unique index on this one...
public class NonindexedComponent : IndexedComponent
{
public NonindexedComponent(){}
}
}
流畅的 NHibernate 映射:
using FluentNHibernate.Mapping;
using Generic.TheDomain;
namespace Persistence.Mappings
{
public class IndexedMap : ClassMap<UseAIndexedComponent>
{
public IndexedMap()
{
Table("IndexedComponent");
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.NameTitle);
Component<IndexedComponent>(x => x.IndexedComponent);
}
}
public class NonIndexedMap : ClassMap<UseANonindexedComponent>
{
public NonIndexedMap()
{
Table("NonIndexedComponent");
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.TitleName);
Component<NonindexedComponent>(x => x.ANonindexedComponent);
}
}
public class IndexedComponentMap : ComponentMap<IndexedComponent>
{
public IndexedComponentMap()
{
Map(x => x.A).CustomSqlType("varchar(1)").UniqueKey("ABC");
Map(x => x.B).CustomSqlType("varchar(1)").UniqueKey("ABC");
Map(x => x.C).CustomSqlType("varchar(1)").UniqueKey("ABC");
}
}
public class NonindexedComponentMap : ComponentMap<NonindexedComponent>
{
public NonindexedComponentMap()
{
Map(x => x.A).CustomSqlType("varchar(1)");
Map(x => x.B).CustomSqlType("varchar(1)");
Map(x => x.C).CustomSqlType("varchar(1)");
}
}
}
和测试:
[Test]
public void VerifyIndexedMapping()
{
new PersistenceSpecification<UseAIndexedComponent>(Session)
.CheckProperty(c => c.NameTitle, "jack")
.CheckProperty(x => x.IndexedComponent, new IndexedComponent())
.VerifyTheMappings();
}
[Test]
public void VerifyNonIndexedMapping()
{
new PersistenceSpecification<UseANonindexedComponent>(Session)
.CheckProperty(c => c.TitleName, "jill")
.CheckProperty(x => x.ANonindexedComponent, new NonindexedComponent())
.VerifyTheMappings();
}
Am getting an error on VerifyTheMapping test:
System.ApplicationException: For property 'ANonindexedComponent'
expected 'Generic.TheDomain.NonindexedComponent' of type
'Generic.TheDomain.NonindexedComponent' but got '' of type
'Generic.TheDomain.NonindexedComponent' and
System.ApplicationException: For property 'IndexedComponent' expected
'Generic.TheDomain.IndexedComponent' of type
'Generic.TheDomain.IndexedComponent' but got '' of type
'Generic.TheDomain.IndexedComponent'
given:
namespace Generic.TheDomain
{
public class UseAIndexedComponent:DomainEntity<UseAIndexedComponent>
{
public virtual string NameTitle { get; private set; }
public virtual IndexedComponent IndexedComponent { get; private set; }
}
public class UseANonindexedComponent:DomainEntity<UseANonindexedComponent>
{
public virtual string TitleName { get; private set; }
public virtual NonindexedComponent
ANonindexedComponent { get; private set; }
}
//we want this one to have a unique index on A B C
public class IndexedComponent
{
public virtual string A { get; private set; }
public virtual string B { get; private set; }
public virtual string C { get; private set; }
public IndexedComponent(){}
}
//no unique index on this one...
public class NonindexedComponent : IndexedComponent
{
public NonindexedComponent(){}
}
}
Fluent NHibernate mappings:
using FluentNHibernate.Mapping;
using Generic.TheDomain;
namespace Persistence.Mappings
{
public class IndexedMap : ClassMap<UseAIndexedComponent>
{
public IndexedMap()
{
Table("IndexedComponent");
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.NameTitle);
Component<IndexedComponent>(x => x.IndexedComponent);
}
}
public class NonIndexedMap : ClassMap<UseANonindexedComponent>
{
public NonIndexedMap()
{
Table("NonIndexedComponent");
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.TitleName);
Component<NonindexedComponent>(x => x.ANonindexedComponent);
}
}
public class IndexedComponentMap : ComponentMap<IndexedComponent>
{
public IndexedComponentMap()
{
Map(x => x.A).CustomSqlType("varchar(1)").UniqueKey("ABC");
Map(x => x.B).CustomSqlType("varchar(1)").UniqueKey("ABC");
Map(x => x.C).CustomSqlType("varchar(1)").UniqueKey("ABC");
}
}
public class NonindexedComponentMap : ComponentMap<NonindexedComponent>
{
public NonindexedComponentMap()
{
Map(x => x.A).CustomSqlType("varchar(1)");
Map(x => x.B).CustomSqlType("varchar(1)");
Map(x => x.C).CustomSqlType("varchar(1)");
}
}
}
and the test:
[Test]
public void VerifyIndexedMapping()
{
new PersistenceSpecification<UseAIndexedComponent>(Session)
.CheckProperty(c => c.NameTitle, "jack")
.CheckProperty(x => x.IndexedComponent, new IndexedComponent())
.VerifyTheMappings();
}
[Test]
public void VerifyNonIndexedMapping()
{
new PersistenceSpecification<UseANonindexedComponent>(Session)
.CheckProperty(c => c.TitleName, "jill")
.CheckProperty(x => x.ANonindexedComponent, new NonindexedComponent())
.VerifyTheMappings();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
http://www.nhforge.org/doc/nh /en/index.html#persistent-classes-equalshashcode
http://www.nhforge.org/doc/nh/en/index.html#persistent-classes-equalshashcode