如何以正确的方式映射集合

时间:2013-07-07 14:28:19

标签: nhibernate nhibernate-mapping

在NHibernate中映射对象集合的最佳方法是什么? 现在我正在使用包,但也许另一种方法可以更有效率?

1 个答案:

答案 0 :(得分:0)

在穿Bag和ClassMapping之前,有一些与此类似的东西:

        Bag(am => am.AlumnoMateriaId, map =>
            {
                map.Table("Calificacion");
                map.Cascade(Cascade.None);
                map.Key(k =>
                    {
                        k.Column("AlumnoId");
                        k.ForeignKey("FK_Calificacion_AlumnoMateria");
                    });
            }, rel => rel.ManyToMany(p => p.Column("AlumnoId")));

现在使用NHibernate.Mapping.Attributes,类看起来像:

使用NHMA = NHibernate.Mapping.Attributes;

[NHMA.Class(Table =“Calificacion”)]

公共课Calificacion     {         [NHMA.Id(0,Name =“id”,TypeType = typeof(Int32),Column =“Id”,Access =“field”)]         [NHMA.Generator(1,Class =“native”)]         public Int32 id;

    public virtual Int32 Id
    {
        get { return id; }
        set { id = value; }
    }

    [NHMA.ManyToOne(2, Name = "calificacion", Access = "field", Column = "CalificacionID",
    Class = "Calificacion", ClassType = typeof(Calificacion),
    ForeignKey = "FK_Calificacion_Alumno", NotNull = false)]
    private Calificacion calificaion;

    public virtual Calificacion Calificaion
    {
        get { return calificaion; }
        set { calificaion = value; }
    }



    [NHMA.Property(Name = "nombre", Access = "field", Column = "Nombre", Length = 50)]
    private string nombre;

    public virtual string Nombre
    {
        get { return nombre; }
        set { nombre = value; }
    }

}