Fluent NHibernate Many to one mapping

后端 未结 3 1059
梦谈多话
梦谈多话 2021-01-02 17:15

I am new to Hibernate world. It may be a silly question, but I am not able to solve it. I am testing many to One relationship of tables and trying to insert record. I have a

3条回答
  •  佛祖请我去吃肉
    2021-01-02 17:40

    Mattias' answer is almost right, but ForeignKey is used for schema generation. Try the below mapping instead. Also, you have the Employees collection mapped with CascadeAll. This will delete employee records if you delete a department, which is probably not desirable.

    public class DeptMapping : ClassMap
        { 
            public DeptMapping() 
            {
                Id(x => x.Id); 
                Map(x => x.DeptName); 
                Map(x => x.DeptLocation); 
                HasMany(x => x.Employees).KeyColumn("DeptId").Inverse().Cascade.All(); 
            } 
        }
    
    public class EmployeeMapping : ClassMap
    { 
        public EmployeeMapping() 
        { 
            Id(x => x.Id); 
            Map(x => x.EmpName); 
            Map(x => x.EmpAge); 
            Map(x => x.DeptId); 
            References(x => x.Dept, "DeptId").Cascade.None(); 
       } 
    }
    

提交回复
热议问题