Fluent NHibernate使用作为组件的键映射字典

时间:2011-10-12 00:02:32

标签: c# .net nhibernate fluent-nhibernate

我已经创建了以下两个类及其各自的映射:

public class ParentClass
{
    public int Id { get; set; }
    public IDictionary<YearMonth, ChildClass> SomeMap { get; set; }
}

public class ParentClassMap : ClassMap<ParentClass>
{
    public ParentClassMap()
    {
        Table("ParentClass");
        Id(x => x.Id);
        HasMany(x => x.SomeMap)
            .Table("ChildClass")
            .KeyColumn("ParentId")
            .AsMap(x => x.YearMonth);
    }
}

public class ChildClass
{
    public int Id { get; set; }
    public YearMonth YearMonth { get; set; }
}

public class ChildClaassMap : ClassMap<ChildClass>
{
    public ChildClaassMap()
    {
        Table("ChildClass");
        Id(x => x.Id);
        Component(x => x.YearMonth, x =>
        {
            x.Map(Reveal.Member<YearMonth>("_dateTime"))
                .Access.Field()
                .Column("`Date`");
        });
    }
}

但是,在测试ParentClass的映射时,会抛出以下错误:

[TestFixture]
public class ParentClassMapTests : IntegrationTestBase
{
    [Test]
    public void Should_be_retrievable()
    {
        new PersistenceSpecification<ParentClass>(GetSession(), new PersistentObjectEqualityComparer())
            .CheckProperty(Reveal.Member<ParentClass>("_dataDictionary"), new Dictionary<YearMonth, BenchmarkPieceData> { { new YearMonth(2010, 1), new BenchmarkPieceData(null, new YearMonth(2010, 1), 0) } })
            .VerifyTheMappings();
    }
}


NHibernate.MappingException : Could not determine type for: BenchmarkPlus.CommonDomain.ValueObjects.YearMonth, BenchmarkPlus.CommonDomain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null, for columns: NHibernate.Mapping.Column(YearMonth)
at NHibernate.Mapping.SimpleValue.get_Type()
at NHibernate.Mapping.SimpleValue.IsValid(IMapping mapping)
at NHibernate.Mapping.IndexedCollection.Validate(IMapping mapping)
at NHibernate.Cfg.Configuration.ValidateCollections()
at NHibernate.Cfg.Configuration.Validate()
at NHibernate.Cfg.Configuration.BuildSessionFactory()
NHibernate\SessionFactoryBuilder.cs(24,0): at BenchmarkPlus.RMS.Infrastructure.NHibernate.SessionFactoryBuilder.BuildFactory()
NHibernate\SessionFactoryBuilder.cs(13,0): at BenchmarkPlus.RMS.Infrastructure.NHibernate.SessionFactoryBuilder..ctor(IConfigurationFactory configurationFactory)

如果类型从YearMonth更改为DateTime,则映射可以正常工作。我想我能用我的YearMonth类型作为键映射到IDictionary吗?如果是这样,我做错了什么?

0 个答案:

没有答案