简体   繁体   English

NHibernate实体和多个关联

[英]NHibernate Entities and Multiple Associations

I don't know how to phrase the question properly so sorry in advance. 我不知道如何正确地说出这个问题,所以提前抱歉。

Using FluentNHibernate in particular, how would you go about making your entities for some tables that have are being referenced by a LOT of other tables? 特别是使用FluentNHibernate,你会如何为一些已经被很多其他表引用的表创建实体?

For example an Employee entity. 例如,Employee实体。 Employees are generally used almost everywhere and it would make sense for some programs to have their Employees contain multiple Roles, multiple Tasks, or multiple benefits. 员工通常在几乎所有地方都使用,一些程序让员工包含多个角色,多个任务或多个好处是有意义的。 Right now I'm picturing it to be like this: 现在我想象它是这样的:

public class Employee
{
    public virtual Employee()
    {
        Tasks = new List<Task>();
        Roles = new List<Role>();
        Benefits = new List<Benefit>();
    }

    public virtual Id { get; set; }
    public virtual Name { get; set; }

    public virtual IList<Task> Tasks { get; protected set; }
    public virtual IList<Role> Roles { get; protected set; }
    public virtual IList<Benefit> Benefits { get; protected set; }

    public virtual void AddTask(Task task)
    {
        task.Employee = this;
        Tasks.Add(task);
    }
    public virtual void AddBenefit(Benefit benefit)
    {
        benefit.Employee = this;
        Benefits.Add(benefit);
    }
    public virtual void AddBenefit(Benefit benefit)
    {
        benefit.Employee = this;
        Benefits.Add(benefit);
    }
}

public class EmployeeMapper : ClassMap<Employee>
{
    public EmployeeMapper()
    {
        id( x => x.Id );
        Map( x => x.Name );
        HasMany( x => x.Tasks );
        HasMany( x => x.Roles );
        HasMany( x => x.Benefits );
    }
}

Now whiel in this example there's just 3 collections, there's the possibility that it'll grow and grow with every association made to employee. 现在,在这个例子中,只有3个集合,它可能随着与员工的每个关联而成长和成长。 Like in a Human Resources System, it'll be associated with everything from Payroll, Addresses, Taxes, Deductions, etc. Wouldn't it get too overblown if I make a collection for every small reference I call? 就像在人力资源系统中一样,它将与工资单,地址,税收,扣除等所有内容相关联。如果我为我称之为的每个小参考制作一个集合,它会不会太夸张?

Is there a pattern to avoid this scenario of coming or is NHibernate designed so that I have to reference every single object that has a foreign key constraint on my objects? 是否有一种模式可以避免这种情况的发生,或者是NHibernate的设计,以便我必须引用每个对象都有一个外键约束?

The employee does not have to know about every fk reference to itself. 员工不必知道每个fk对自己的引用。 Only do that for the ones you actually need to reference through the employee. 只对那些您实际需要通过员工引用的人执行此操作。

in a relationship where both sides are mapped, NHibernate needs you to set both sides before it will save correctly. 在双方都被映射的关系中,NHibernate需要你设置双方才能正确保存。

That simply means that IF you map both sides, then you need to explicitly set the properties on both sides like 这只是意味着如果你映射两面,那么你需要明确设置两侧的属性,如

parent.children.Add(child); 
child.parent = parent;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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