流畅的 Nhibernate 映射两个数据库表上的单个类
我在映射方面遇到问题。
我的数据库中有两个表,如下所示: Employee 和 EmployeeManagers
Employee
EmployeeId int 名称 nvarchar
EmployeeManagers
EmployeeIdFk int ManagerIdFk int
因此员工可以有 0 个或多个经理。经理本身也是员工。
我有以下类来代表员工和经理
public class Employee
{
public virtual int Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual IList<Employee> Managers
{
get;
protected set;
}
public Employee()
{
Managers = new List<Employee>();
}
}
我没有任何类来代表经理,因为我认为没有必要,因为经理本身就是一名员工。
我正在使用 autoMapping,但我只是不知道如何将此类映射到这两个表。我正在实现 IAutoMappingOverride 来覆盖 Employee 的自动映射,但我不确定在其中做什么。
public class NodeMap : IAutoMappingOverride
{
public void Override(AutoMapping<Node> mapping)
{
//mapping.HasMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
//mapping.HasManyToMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
}
}
我还想确保不能为同一名员工分配两次同一经理。这是我可以在我的应用程序中验证的内容,但我想对 EmployeeManager 表(例如复合键)施加约束,以便同一经理不能多次分配给员工。
有人可以帮我解决这个问题吗?
等待中 纳比尔
I am having problems with Mapping.
I have two tables in my database as follows: Employee and EmployeeManagers
Employee
EmployeeId int
Name nvarchar
EmployeeManagers
EmployeeIdFk int
ManagerIdFk int
So the employee can have 0 or more Managers. A manager itself is also an Employee.
I have the following class to represent the Employee and Managers
public class Employee
{
public virtual int Id
{
get;
set;
}
public virtual string Name
{
get;
set;
}
public virtual IList<Employee> Managers
{
get;
protected set;
}
public Employee()
{
Managers = new List<Employee>();
}
}
I don't have any class to represent Manager because I think there is no need for it, as Manager itself is an Employee.
I am using autoMapping and I just can't figure out how to map this class to these two tables. I am implementing IAutoMappingOverride for overriding automappings for Employee but I am not sure what to do in it.
public class NodeMap : IAutoMappingOverride
{
public void Override(AutoMapping<Node> mapping)
{
//mapping.HasMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
//mapping.HasManyToMany(x => x.ValidParents).Cascade.All().Table("EmployeeManager");
}
}
I also want to make sure that an employee can not be assigned the same manager twice. This is something I can verify in my application but I would like to put constraint on the EmployeeManager table (e.g. a composite key) so a same manager can not be assigned to an employee more than once.
Could anyone out there help me with this please?
Awaiting
Nabeel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我做了类似的事情,也许它可以帮助你开始?
http://www.dbones。 co.uk/blog/post/2010/04/Nhib-Self-Reference-Object.aspx
编辑opps我可以看到它的多个管理器
Bones
I did something like this, maybe it could help you start out?
http://www.dbones.co.uk/blog/post/2010/04/Nhib-Self-Reference-Object.aspx
Edit opps i can see its multiple managers
Bones
我最终这样做
I end up doing like this