【问题标题】:Fluent NHibernate one to many am i doing it right?Fluent NHibernate 一对多我做对了吗?
【发布时间】:2015-09-01 15:41:52
【问题描述】:

有图书课。

public class Books
{

    public virtual int Id { get; set; }
    public virtual string Title { get; set; }

    public virtual string Category { get; set; }

    public virtual string ISBN { get; set; }

    public virtual string Description { get; set; }

    public virtual string Image { get; set; }

    public virtual int CategoryId { get; set; }

    public Categories Categories { get; set; }

    public virtual IList<Comments> Comments { get; set; }

    public Books()
    {
        Comments = new List<Comments>();
    }

}

还有评论类。

public class Comments    
{
    public virtual int Id { get; set; }

    public virtual string CommentText { get; set; }

    public virtual DateTime Date { get; set; }

    public virtual int IdBook { get; set; }

    public Books Books { get; set; }
}

我的图书类地图代码:

public class BooksMap : ClassMap <Books>
{
    public BooksMap()
    {
        Id(x => x.Id);
        Map(x => x.Title);
        Map(x => x.ISBN);
        Map(x => x.Description);
        Map(x => x.Category);
        Map(x => x.Image);
        Map(x => x.CategoryId);
        //reference to categories
        References(x => x.Categories).Column("Id").Not.Nullable();
        //inverse reference (hasmany comments, rating)
        HasMany(x => x.Comments).Cascade.All().Inverse();
    }
}

还有评论地图>

public class CommentsMap:ClassMap<Comments>
{
    public CommentsMap()
    {
        Id(x => x.Id);
        Map(x => x.CommentText);
        Map(x => x.Date);
        Map(x => x.IdBook);
        References(x => x.Books).Column("Id").Not.Nullable();
    }
}

我的问题是:我做得对吗? - 以及如何使用此映射进行查询,例如标准语言?

【问题讨论】:

    标签: c# asp.net-mvc nhibernate fluent-nhibernate fluent-nhibernate-mapping


    【解决方案1】:

    很高兴知道什么不起作用(而不是询问"is this ok?"

    有一些总结,正确映射一对多和多对一的方法是什么:

    Minimal and correct way to map one-to-many with NHibernate

    无论如何,所有属性都必须是虚拟的

    public class Books
    {
        public virtual int Id { get; set; }
        ...
    
        public virtual int CategoryId { get; set; }
        // all must be virtual
        //public Categories Categories { get; set; }
        public virtual Categories Categories { get; set; }
    
        public virtual IList<Comments> Comments { get; set; }
    

    我们还可以看到,CategoryIdCategory - 一列的双重映射(一次作为参考,一次作为 ValueType)。这意味着,其中之一必须是只读的:

    public BooksMap()
    {
        Id(x => x.Id);
        ...
        // this should be readonly
        // Map(x => x.CategoryId);
        Map(x => x.CategoryId)
           .Not.Update()
           .Not.Insert();
        //reference to categories
        References(x => x.Categories)...
    

    引用应映射到表示 Categroy_ID 而不是 Id 的列(已用于属性 x.Id)

    // this does not seem to be ok
    References(x => x.Categories).Column("Id").Not.Nullable();
    // we would need different column
    References(x => x.Categories)
        .Column("Category_ID")
        .Not.Nullable();
    

    我还期望在Comments 中有一些名为"Book_ID" 的列。这是列,我们将用于HasMany() 映射。

    HasMany(x => x.Comments)
        .KeyColumn("Book_ID")
        .Cascade.AllDeleteOrphan()
        .Inverse();
    

    另一侧必须使用同一列“Book_ID”

    public CommentsMap()
    {
        Id(x => x.Id);
        ...
        References(x => x.Books)
            .Column("Book_ID")
            .Not.Nullable();
    }
    

    所以,这个映射现在应该可以使用了。要了解有关查询的一些想法,请查看包含大量示例的文档:

    【讨论】:

    • 我应该先映射 Book_ID 还是应该在映射 cmets 时添加对其他表的引用?这:References(x =&gt; x.Books) .Column("Book_ID") .Not.Nullable(); 或:Map(x=&gt;x.Book_ID); 然后References(x =&gt; x.Books) .Column("Book_ID") .Not.Nullable();
    • 映射文件的顺序并不重要。一个 (References) 属于 Comment 映射,而第二个 (HasMany) 属于 Book 映射。两者都是必需的,NHibernate 会按正确的顺序为我们处理它们...希望对您有所帮助