流利的nhibernate m-to-m映射与外部表 [英] Fluent nhibernate m-to-m mapping with external table
问题描述
实体
----------
** EntityId ** NUMBER(9),** EntityName ** VARCHAR2
EntityLinks
--------------
* * EntityLinkId ** NUMBER(9),** ParentEntityId ** NUMBER(9),** ChildEntityId ** NUMBER(9)
Table EntityLinks将存储各种实体之间的ManyToMany关系。 ParentEntityId 和 ChildEntityId 与实体具有外键关系。
一个实体的下面的类以及
public class实体
{
public virtual int EntityId {get; set}
公共虚拟IList< Entity> ParentEntities {get; set}
公共虚拟IList< Entity> ChildEntities {获得; set}
}
public class EntityLinks
{
public virtual int EntityLinkId {get;设置}
公共虚拟实体ParentEntityId {get; set}
public virtual Entity ChildEntityId {get;设置}
}
这两个类的映射:
public class EntityMap:ClassMap< Entity>
{
public EntityMap()
{
表(实体)
Id(x => x.EntityId).GeneratedBy.Increment();
* ----如何映射ParentEntities和ChildEntites?---- *
}
}
public class EntityLinksMap:ClassMap< Entity>
{
public EntityMap()
{
表(实体)
Id(x => x.EntityId).GeneratedBy.Increment();
引用(x => x.ParentEntityId).Column(ParentEntityId);
引用(x => x.ChildEntityId).Column(ChildEntityId);
$ b $ p
$ b我的问题是我应该如何在实体类中进行映射对于ParentEntities和ChildEntites,以便我得到一个特定实体的父母和孩子的列表?被使用。因为这是自引用的,所以我只是对这个问题感到困惑。
$ $ $ $ $ $ $ $ $ $ public class EntityMap:ClassMap< Entity>
{
public EntityMap()
{
表(实体)
Id(x => x.EntityId).GeneratedBy.Increment();
//解决方案映射
HasManyToMany(x => x.ChildEntities)
.Table(EntityLinks)
.ParentKeyColumn(ParentEntityId)
.ChildKeyColumn(ChildEntityId);
HasManyToMany(x => x.ParentEntities)
.Table(EntityLinks)
.ParentKeyColumn(ChildEntityId)
.ChildKeyColumn(ParentEntityId);
public class EntityLinksMap:ClassMap< EntityLinks>
{
public EntityMap()
{
表(EntityLinks)
Id(x => x.EntityId).GeneratedBy.Increment();
引用(x => x.ParentEntityId).Column(ParentEntityId);
引用(x => x.ChildEntityId).Column(ChildEntityId);
}
}
I have two tables in Oracle
Entity
----------
**EntityId** NUMBER(9), **EntityName** VARCHAR2
EntityLinks
--------------
**EntityLinkId** NUMBER(9),**ParentEntityId** NUMBER(9), **ChildEntityId** NUMBER(9)
Table EntityLinks will store ManyToMany relationship between various entities. ParentEntityId and ChildEntityId are having foreign key relationship with Entity.
I have below a below class for Entity as well
public class Entity
{
public virtual int EntityId {get; set}
public virtual IList<Entity> ParentEntities {get; set}
public virtual IList<Entity> ChildEntities{get; set}
}
public class EntityLinks
{
public virtual int EntityLinkId {get; set}
public virtual Entity ParentEntityId {get; set}
public virtual Entity ChildEntityId {get; set}
}
Here is the mapping for both the classes:
public class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
*---- How to map for ParentEntities and ChildEntites?----*
}
}
public class EntityLinksMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
References(x=>x.ParentEntityId).Column("ParentEntityId");
References(x=>x.ChildEntityId).Column("ChildEntityId");
}
}
My question is how should I do mapping in entity class for ParentEntities and ChildEntites so that I get the list of both parent and child for a particular entity?
I figured out the mappings to be use. I was just confused in the problem at hand since this is self-referencing.
public class EntityMap : ClassMap<Entity>
{
public EntityMap()
{
Table("Entity")
Id(x=>x.EntityId).GeneratedBy.Increment();
//solution mapping
HasManyToMany(x => x.ChildEntities)
.Table("EntityLinks")
.ParentKeyColumn("ParentEntityId")
.ChildKeyColumn("ChildEntityId");
HasManyToMany(x => x.ParentEntities)
.Table("EntityLinks")
.ParentKeyColumn("ChildEntityId")
.ChildKeyColumn("ParentEntityId");
}
}
public class EntityLinksMap : ClassMap<EntityLinks>
{
public EntityMap()
{
Table("EntityLinks")
Id(x=>x.EntityId).GeneratedBy.Increment();
References(x=>x.ParentEntityId).Column("ParentEntityId");
References(x=>x.ChildEntityId).Column("ChildEntityId");
}
}
这篇关于流利的nhibernate m-to-m映射与外部表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!