0

我有两个类,每个类都有一个域和一个 repo 版本。

领域:

public class MusicInfo
{
    public string Id { get; set; }
    public MusicImage Image { get; set; }

    public MusicInfo(byte[] image)
    {
        this.Image = new MusicImage(this, image);
    }
}

public class MusicImage
{
    public byte[] Blob { get; set; }
    public MusicInfo MusicInfo { get; set; }
    public string Id { get; set; }

    public MusicImage(MusicInfo musicInfo, byte[] blob)
    {
        if (musicInfo == null)
            throw new ArgumentNullException("musicInfo");
        if (blob == null)
            throw new ArgumentNullException("blob");

        this.MusicInfo = musiscInfo;
        this.Blob = blob;
    }
}

回购:

public class MusicInfoRepo
{
    public virtual long Id { get; set; }
    public virtual MusicImageRepo Image { get; set; }
}

public class MusicImageRepo
{
    public virtual byte[] Blob { get; set; }
    public virtual MusicInfoRepo MusicInfo { get; set; }
    public virtual long Id { get; set; }
}

这是他们的映射:

public class MusicInfoRepoMap : HighLowClassMapping<MusicInfoRepo>
{
    public MusicInfoRepoMap()
    {
        Table("MusicInfo");

        Id(f => f.Id, m => m.Generator(Generators.HighLow, HighLowMapper));

        OneToOne(f => f.Image, m => m.Cascade(Cascade.All));
    }
}

public class MusicImageRepoMap : ClassMapping<MusicImageRepo>
{
    public MusicImageRepoMap()
    {
        Table("MusicImage");

        Id(f => f.Id, m => m.Generator(Generators.Foreign<MusicImageRepo>(f => f.MusicInfo)));

        Property(f => f.Blob, m =>
        {
            m.NotNullable(true);
            m.Column(c => c.SqlType("VARBINARY(MAX)"));
            m.Length(Int32.MaxValue); 
            m.Update(false);
        });

        OneToOne(f => f.MusicInfo,
        m =>
        {
            m.Cascade(Cascade.None);
            m.Constrained(true);
            m.Lazy(LazyRelation.NoLazy);
        });
    }
}

当我尝试查询与 ClassB 具有一对一关系且与 MusicInfo 具有一对一关系的 ClassA 时,会出现错误消息:

Missing type map configuration or unsupported mapping.

Mapping types:
MusicImageRepo -> Byte[]
Blah.MusicImageRepo -> System.Byte[]

Destination path:
List`1[0]

Source value:
Blah.MusicImageRepo

但这是我映射它们的方式:

Mapper.CreateMap<MusicInfo, MusicInfoRepo>();
Mapper.CreateMap<MusicInfoRepo, MusicInfo>();

Mapper.CreateMap<MusicImage, MusicImageRepo>();
Mapper.CreateMap<MusicImageRepo, MusicImage>();

保存这些类时没有问题。我真的不明白为什么会发生错误。非常感谢您的帮助。

4

1 回答 1

0

OneToOne 表示另一个实体/表具有参考(列)。当双方都有一个onetoone映射时,它不应该工作,因为他们来回反弹责任。

此外,当您只需要将字节存储在其他地方时,这些类看起来有点过于复杂。

public class MusicInfoRepo
{
    public virtual long Id { get; private set; }
    public virtual MusicImageRepo Image { get; private set; }

    public MusicInfo(byte[] image)
    {
        this.Image = new MusicImageRepo(this, image);
    }
}

public class MusicImageRepo
{
    public virtual MusicInfoRepo MusicInfo { get; private set; }
    public virtual byte[] Blob { get; set; }
}

public class MusicInfoRepoMap : HighLowClassMapping<MusicInfoRepo>
{
    public MusicInfoRepoMap()
    {
        Table("MusicInfo");

        Id(f => f.Id, m => m.Generator(Generators.HighLow, HighLowMapper));

        OneToOne(f => f.Image, m => m.Cascade(Cascade.All));
    }
}

public class MusicImageRepoMap : ClassMapping<MusicImageRepo>
{
    public MusicImageRepoMap()
    {
        Table("MusicImage");

        ComposedId(m => m.ManyToOne(x => x.MusicInfo));

        Property(f => f.Blob, m =>
        {
            m.NotNullable(true);
            m.Column(c => c.SqlType("VARBINARY(MAX)"));
            m.Length(Int32.MaxValue); 
            m.Update(false);
        });
    }
}

注意:如果 DomainModel 和 MappedModel 之间的分离真的有意义,请考虑一下。NHibernate 竭尽全力支持 DomainModels 的映射。

于 2013-12-10T12:01:39.843 回答