如何将Fluent NHibernate映射转换为NHibernate内置基于代码的映射

时间:2012-06-18 08:48:32

标签: c# nhibernate nhibernate-mapping

我使用Fluent NHibernate映射的示例中有两个类。流畅的NHibernate映射被注释,我试图进行基于代码的映射,但仍然出现ArgumentNullException“值不能为空。”。怎么做对了?

//Fluent NHIbernate mapping for table LocalizationEntry
//public class LocalizationEntryMapping : ClassMap<LocalizationEntry>
//{
//    public LocalizationEntryMapping()
//    {
//        Cache.ReadWrite();
//        CompositeId()
//            .ComponentCompositeIdentifier(x => x.Id)
//            .KeyProperty(x => x.Id.Culture)
//            .KeyProperty(x => x.Id.EntityId)
//            .KeyProperty(x => x.Id.Property)
//            .KeyProperty(x => x.Id.Type);
//        Map(x => x.Message);
//    }
//}
public class LocalizationEntryId
{
    public virtual string Culture { get; set; }
    public virtual string Type { get; set; }
    public virtual string Property { get; set; }
    public virtual string EntityId { get; set; }

    public override bool Equals(object obj)
    {
        if (obj != null)
        {
            LocalizationEntryId other = obj as LocalizationEntryId;
            if (other != null)
            {
                return this.Type      == other.Type &&
                        this.Property == other.Property &&
                        this.EntityId == other.EntityId &&
                        this.Culture  == other.Culture;
            }
        }
        return false;
    }

    public override int GetHashCode()
    {
        return base.GetHashCode();
    }
}

public class LocalizationEntry : IDomainMapper
{
    public virtual LocalizationEntryId Id { get; set; }
    public virtual string Message { get; set; }

    public virtual void Map(ModelMapper mapper)
    {
        mapper.Class<LocalizationEntry>(m =>
        {                
            m.ComposedId( t =>
            {
                t.Property(g => g.Id.Culture, c =>
                {
                    c.NotNullable(true);
                    c.Length(10);
                });
                t.Property(g => g.Id.EntityId, c =>
                {
                    c.NotNullable(true);
                });
                t.Property(g => g.Id.Property, c =>
                {
                    c.NotNullable(true);
                    c.Length(100);
                });
                t.Property(g => g.Id.Type, c =>
                {
                    c.NotNullable(true);
                    c.Length(100);
                });
            });

            m.Property(t => t.Message, c =>
            {
                c.NotNullable(true);
                c.Length(400);
            });
        });
    }
}

////////////编辑//////////////// 我已经找到了解决方案。映射应该这样做:

public virtual void Map(ModelMapper mapper)
        {
            mapper.Class<LocalizationEntry>(m =>
            {
                m.ComponentAsId(x => x.Id, n =>
                {
                    n.Property(x => x.Culture);
                    n.Property(x => x.EntityId);
                    n.Property(x => x.Property);
                    n.Property(x => x.Type);
                });

                m.Property(t => t.Message, c =>
                {
                    c.NotNullable(true);
                    c.Length(400);
                });
            });
        }

1 个答案:

答案 0 :(得分:1)

我已经找到了解决方案。映射应该这样做:

public virtual void Map(ModelMapper mapper)
    {
        mapper.Class<LocalizationEntry>(m =>
        {
            m.ComponentAsId(x => x.Id, n =>
            {
                n.Property(x => x.Culture);
                n.Property(x => x.EntityId);
                n.Property(x => x.Property);
                n.Property(x => x.Type);
            });

            m.Property(t => t.Message, c =>
            {
                c.NotNullable(true);
                c.Length(400);
            });
        });
    }