Fluent NHibernate 映射组件,一种已索引,一种未索引

发布于 2024-09-27 00:36:16 字数 3566 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文