开发者

Fluent NHibernate mapping a dictionary with a key that is a component

开发者 https://www.devze.com 2023-04-12 21:40 出处:网络
I\'ve created the following two classes with their respective mappings: public class ParentClass { public int Id { get; set; }

I've created the following two classes with their respective mappings:

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

public class ParentClassMap : ClassMap<ParentClass>
{
    public ParentClassMa开发者_运维百科p()
    {
        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`");
        });
    }
}

When testing the mapping for the ParentClass, though, the following error is thrown:

[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)

If the type is changed from a YearMonth to a DateTime, the mapping works fine. Am I suppose to be able to map to an IDictionary with my YearMonth type as the key? If so, what am I doing wrong?

0

精彩评论

暂无评论...
验证码 换一张
取 消