通过代码进行双向NHibernate映射 [英] Bi-directional NHibernate mapping by code

查看:104
本文介绍了通过代码进行双向NHibernate映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请明确说明-这个问题与Fluent NHibernate无关.

Just to be clear - this question is not about Fluent NHibernate.

我有一个ParentChild类,它们之间是一对多的关系.

I have a Parent and Child classes, with a one-to-many relationship between them.

为便于阅读,该代码已缩短.

The code is shortened for readability.

public class Child
{
    int Id;
    string Name;
}

public class Parent
{
    int Id;
    string Name;
    Iesi.Collections.Generic.ISet<Child> Children;
}

public ChildMapping()
{
    Table("Children");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });
}

public ParentMapping()
{
    Table("Parents");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    Set(p => p.Children, m => {
        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Key(k => {
            k.Column("ParentId");
            k.NotNullable(true);
        });
    }, a => a.OneToMany());
}

Child类在其上需要一个Parent属性. Parent需要控制这种关系(我不能在Parent的一端将Inverse设置为true).

The Child class needs a Parent property on it. The Parent needs to control the relationship (I can't set the Inverse to true on the Parent's end).

ParentChild映射应如何显示?

推荐答案

我添加了以下项目:

I've added the following items:

子类:字段_parent,构造函数
父类:AddChild方法
子映射:用于父级属性的多音
父级映射:inverse(true)使子级成为父级处理程序

Child class: a field _parent, a constructor
Parent class: AddChild method
Child mapping: manytoone for parent property
Parent mapping: inverse(true) to make parent handler of the children

完整代码:

public class Child
{
    int Id;
    string Name;
    Parent _parent;

    public Child(Parent parent)
   {
     _parent = parent;
   }
}

public class Parent
{
    int Id;
    string Name;
    Iesi.Collections.Generic.ISet<Child> Children;

    public virtual Child AddChild()
    {
       Child newChild = new Child(this); //link parent to child via constructor
       Children.Add(newChild); //add child to parent's collection
       return newChild; //return child for direct usage
    }
}

public ChildMapping()
{
    Table("Children");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    ManyToOne(x => x.Parent, map => {
         map.Column("Id"); /* id of the parent table */
         map.Access(Accessor.Field);
         map.NotNullable(true);
         map.Class(typeof(Parent));
        });    
}

public ParentMapping()
{
    Table("Parents");

    Id(p => p.Id, m => {
        m.Column("Id");
        m.Generator(Generators.Identity);
    });

    Property(p => p.Name, m => {
        m.Column("Name");
        m.NotNullable(true);
    });

    Set(p => p.Children, m => {
        m.Cascade(Cascade.All | Cascade.DeleteOrphans);
        m.Key(k => {
            k.Column("Id"); /* id of the child table */
            k.NotNullable(true);
            k.Inverse(true); /* makes the parent handle it's childlist */
        });
    }, a => a.OneToMany());
}

应该起作用.

这篇关于通过代码进行双向NHibernate映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆