简体   繁体   中英

nhibernate mapping by code problems

I have a class employee smth like:

  public class Employee{  

    private List<Employee> subord = new List<Employee>(); 
    private int id;
    private string surname;
    public virtual Employee Boss { get; set; }
    public virtual List<Employee> Subbord { get; set; }
    public virtual int Id
    {
        get { return this.id; }
        set { this.id = value; }
    }

     public virtual string Surname
    {
        get { return this.surname; }
        set { this.surname = value; }
    }
 }

I want to map within one table Employee. I could config and run everything, but my public class EmployeeMap : ClassMapping incorrect. Could anyone help me and write it here using mapping by code?

smth like this, i am new in nh, even dont ectually know what each command means

       { this.Table("Employee");
        this.Id(
            x => x.Id,
            m =>
                {
                    m.Generator(Generators.Sequence);
                m.UnsavedValue(0);
            });
        this.Property(x => x.Surname, m => m.Column("surname"));
        this.ManyToOne(x => x.Boss, m => { m.Column("bossid");});
        this.Set(
           x => x.Subbord,
           collectionMapping =>
               {
                   collectionMapping.Table("Employee");
                   collectionMapping.Cascade(Cascade.All);
                   collectionMapping.Inverse(true);

                   collectionMapping.Key(
                       k =>
                           {
                              k.Column("bossid"); 

                               k.ForeignKey("fk_Employee_BossEmployee");
                           });
               },
        map => map.OneToMany());
    }

it is actuaaly enought to translate this Fluent(working) on Mapping-by-code

 Id(x => x.Id).GeneratedBy.Native();

    Map(x => x.Name)
        .Length(200)
        .Not.Nullable();

    References(x => x.ParentCategory)
        .Column("ParentCategoryId")
        .Access.CamelCaseField();

    HasMany(x => x.ChildCategories)
        .Cascade.AllDeleteOrphan()
        .AsSet()
        .KeyColumn("ParentCategoryId")
        .Access.CamelCaseField();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM