在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(){}
}
}
Fluent 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();
}