AutoMapper error: class maps to a datatype (byte[])

364 views Asked by At

I have two classes, each having a domain and a repo version.

DOMAIN:

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;
    }
}

REPO:

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; }
}

And here are their mappings:

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);
        });
    }
}

When I am trying to query for a ClassA that has a one to one relationship with ClassB which also has a one to one relationship with MusicInfo, an error occurs that says:

Missing type map configuration or unsupported mapping.

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

Destination path:
List`1[0]

Source value:
Blah.MusicImageRepo

but here is how i map them:

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

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

There are no problems when saving these classes. I really dont get why the error happens. Will really appreciate your help.

1

There are 1 answers

0
Firo On

OneToOne says that the other entity/table has the Reference(column). It should not work when both sides have a onetoone mapping since they bounce the responsiblity back and forth.

Also the classes look a bit overcomplicated when all you need is to store the bytes somewhere else.

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);
        });
    }
}

Note: think about it if the seperation between DomainModel and MappedModel really makes sense. NHibernate goes to great length supporting mapping of DomainModels.