流畅的 Nhibernate 映射两个数据库表上的单个类

发布于 2024-09-05 14:12:53 字数 1159 浏览 5 评论 0原文

我在映射方面遇到问题。

我的数据库中有两个表,如下所示: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

向地狱狂奔 2024-09-12 14:12:54

我做了类似的事情,也许它可以帮助你开始?

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

寻梦旅人 2024-09-12 14:12:54

我最终这样做

    public abstract class Node
    {
        public virtual int Id
        {
            get;
            set;
        }

        public virtual Node ParentNode
        {
            get;
            set;
        }

        public virtual IList<Node> ChildNodes
        {
            get;
            protected set;
        }

        protected Node()
        {
            ChildNodes = new List<Node>();
        }

        public virtual void AddChildNode( Node childNode )
        {
            childNode.ParentNode = this;
            ChildNodes.Add( childNode );
        }

    }

 public class NodeMap : IAutoMappingOverride<Node>
{
    public void Override( AutoMapping<Node> mapping )
    {
        //self referencing
        //http://stackoverflow.com/questions/1547956/fluent-nhibernate-automappings-with-self-reference
        mapping.References( x => x.ParentNode ).Column( "ParentNodeFk" ).Cascade.SaveUpdate();
        mapping.HasMany( x => x.ChildNodes ).Cascade.SaveUpdate().KeyColumn( "ParentNodeFk" );            

    }
}

I end up doing like this

    public abstract class Node
    {
        public virtual int Id
        {
            get;
            set;
        }

        public virtual Node ParentNode
        {
            get;
            set;
        }

        public virtual IList<Node> ChildNodes
        {
            get;
            protected set;
        }

        protected Node()
        {
            ChildNodes = new List<Node>();
        }

        public virtual void AddChildNode( Node childNode )
        {
            childNode.ParentNode = this;
            ChildNodes.Add( childNode );
        }

    }

 public class NodeMap : IAutoMappingOverride<Node>
{
    public void Override( AutoMapping<Node> mapping )
    {
        //self referencing
        //http://stackoverflow.com/questions/1547956/fluent-nhibernate-automappings-with-self-reference
        mapping.References( x => x.ParentNode ).Column( "ParentNodeFk" ).Cascade.SaveUpdate();
        mapping.HasMany( x => x.ChildNodes ).Cascade.SaveUpdate().KeyColumn( "ParentNodeFk" );            

    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文