0

I have two classes MyArticle and Image.

public class MyArticle : IArticle{
    ....
    public virtual List<Image> Images {get; set;}
}
public class Image{
    ...
    public IArticle Article {get; set;}
}

using nhibernate mapping by code I mapped those

public class ImageMap : ClassMapping<Image>
    {
        public ImageMap()
        {            
            ManyToOne(x => x.Article, m =>
            {
                m.NotNullable(true);
            });
        }
}

public class MyArticleMap: ClassMapping<MyArticle>
        {
            public MyArticleMap()
            {            
                Bag(x => x.Images,
              c => { },
              r => { r.OneToMany(); }
            );

            }
    }

When tried to unit test mapping it failes with error {"An association from the table Image refers to an unmapped class: MyApp.Model.IArticle"}

user1765862
  • 13,635
  • 28
  • 115
  • 220

2 Answers2

2

Try this:

public class ImageMap : ClassMapping<Image>
{
    public ImageMap()
    {            
        ManyToOne(x => x.Article, m =>
        {
            m.NotNullable(true);
            m.Class(typeof(MyArticle));
        });
    }
}
Najera
  • 2,869
  • 3
  • 28
  • 52
  • is this means that I should list all objects which implements IArticle interface inside m.Class? If yes, can you provide example of one more type – user1765862 Apr 24 '14 at 12:50
  • If you have a list of implementations, you need to reference the base class, and read about http://www.nhforge.org/doc/nh/en/index.html#inheritance – Najera Apr 24 '14 at 15:12
0

This mapping is wrong

public class MyArticleMap: ClassMapping<MyArticleMap>{...}

It should be

public class MyArticleMap: ClassMapping<MyArticle>{...}

And also the list of Images should be of Interface type like this.

public virtual IList<Image> Images {get; set;}
  • ArticleMap as parameter is typo, I pasted here by mistake. Error remains. – user1765862 Apr 23 '14 at 20:08
  • This should probably be a comment as the first error is quite likely to be a typo and the second point is a style issue. – T. Kiley Apr 23 '14 at 20:29
  • I think this post explains the same problem http://stackoverflow.com/questions/2029436/can-fluent-nhibernates-automapper-handle-interface-types – einsteine89 Apr 23 '14 at 20:47