nhibernate映射表Image中的关联指向未映射的类

本文关键字:映射 关联 Image nhibernate | 更新日期: 2023-09-27 17:59:56

我有两个类MyArticle和Image。

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

通过代码使用nhibernate映射,我映射了那些

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

当尝试对映射进行单元测试时,失败并出现错误{"表Image中的关联指的是未映射的类:MyApp.Model.IArticle"}

nhibernate映射表Image中的关联指向未映射的类

试试这个:

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

此映射是错误的

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

应该是

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

而且图像列表应该是这样的接口类型。

public virtual IList<Image> Images {get; set;}