Reputation: 221
I have a hierarchical (tree structure) SQL Server table TEmployee with following columns
Id bigint identity(1,1) not null
FirstName nvarhcar(50) not null
LastName nvarchar(50) not null
ManagerId bignint null
Column Id is primary key. Each employee has or has a manager; if he/she does, then the value of column ManagerId of his/her self references to a value Id of another manager employee row. For Instance some entries for table TEmployee:
1 John Doe null
2 Jane Smith 1
3 Keith Johnson 1
4 Fox Lynson 2
5 Kim McFeinstein 4
...
Employee John Doe does not have manager. But he is manager of Jane Smith and Keith Johnson. Kim McFeinstein's manager is Fox Lynson.
I would like to know how to write a domain model class and a mapping class using Fluent NHibernate mapping. Also, I'd like, in mapping class' codes, to know the good way for cascading deletion. In other words, if I delete John Doe, then all of its children (employees managed by John Doe) will be deleted recursively, too.
Please help. Thank you.
Domain Model Class but I am not sure about its source codes:
public class Employee
{
public virtual long Id {get;set;}
public virtual string FirstName {get;set;}
public virtual string LastName {get;set;}
public virtual Employee Manager {get;set;} // not sure here ???
}
Mapping class but I am not sure about its source codes, too:
public class EmployeeMap : MapClass<Employee>
{
Table("TEmployee");
SchemaAction.None();
// not sure about following codes
???
}
Upvotes: 0
Views: 209
Reputation: 30813
public class Employee
{
public virtual long Id {get; private set;}
public virtual string FirstName {get;set;}
public virtual string LastName {get;set;}
public virtual Employee Manager {get;set;}
public virtual ICollection<Employee> ManagedEmployees {get; private set;}
}
public class EmployeeMap : MapClass<Employee>
{
Table("TEmployee");
SchemaAction.None();
Id(e => e.Id).GeneratedBy.HiLow("1000");
References(e => e.Manager, "ManagerId");
HasMany(e => e.Manager).KeyColumn("ManagerId").Cascade.All();
}
Upvotes: 1