【问题标题】:Fluent nhibernate m-to-m mapping with external tableFluent nhibernate m-to-m 与外部表的映射
【发布时间】:2015-06-07 10:50:27
【问题描述】:

我在 Oracle 中有两个表

Entity
----------
**EntityId** NUMBER(9), **EntityName** VARCHAR2

EntityLinks
--------------
**EntityLinkId** NUMBER(9),**ParentEntityId** NUMBER(9), **ChildEntityId** NUMBER(9)

Table EntityLinks 将存储各种实体之间的多对多关系。 ParentEntityIdChildEntityIdEntity 具有外键关系。

我也有下面的实体类

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}
}

这是两个类的映射:

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");
  }
}

我的问题是我应该如何在 ParentEntities 和 ChildEntites 的实体类中进行映射,以便获得特定实体的父子列表?

【问题讨论】:

    标签: nhibernate fluent-nhibernate mapping fluent-nhibernate-mapping


    【解决方案1】:

    我会说这真的很好,您使用中间人表作为标准映射实体。

    我说的是这样一个事实,即我们可以使用映射而无需将该配对表表示为映射实体。语法为HasManyToMany

    但是由于您选择将配对表作为一个实体,您必须更改业务模型:

    public class Entity
    {
         public virtual int EntityId {get; set}
         //public virtual IList<Entity> ParentEntities {get; set}
         //public virtual IList<Entity> ChildEntities{get; set}
         public virtual IList<EntityLinks> ParentEntityLinks {get; set}
         public virtual IList<EntityLinks> ChildEntityLinks {get; set}
    }
    

    那么映射将是:

    public EntityMap()
    {
         Table("Entity")
         Id(x=>x.EntityId).GeneratedBy.Increment();
    
         HasMany(x => x.ParentEntityLinks)...
         HasMany(x => x.ChildEntityLinks)...
    }
    

    更多关于HasMany映射:

    Mapping-by-Code - Set and Bag 亚当酒吧

    向下滚动到 Fluent NHibernate 的等效项

    HasMany(x => x.Users)
        .AsSet<CustomComparer>() // or .AsSet(), .AsBag()
        .Fetch.Join()
        .BatchSize(100)
        .LazyLoad() // or .ExtraLazyLoad()
        .Table("tableName")
        .Schema("schemaName")
        .Cascade.AllDeleteOrphan() // or .None(), .SaveUpdate(), .All(), DeleteOrphan()
        .Inverse()
        ...
        // and many more settings
        ...
    

    如果事实上,你真的想保留多对多和类似的属性

     public virtual IList<Entity> ParentEntities {get; set}
     public virtual IList<Entity> ChildEntities{get; set}
    

    映射将是

    HasManyToMany(x => x.ParentEntities)...
    HasManyToMany(x => x.ChildEntities)...
    

    ...我不建议使用:How to create NHibernate HasManyToMany relationNhibernate: How to represent Many-To-Many relationships with One-to-Many relationships?这里有更多详细信息如何使用HasManyToMany if ...

    【讨论】:

      【解决方案2】:

      我想出了要使用的映射。我只是对手头的问题感到困惑,因为这是自引用的。

      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");
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-21
        • 2011-06-18
        • 2012-04-03
        相关资源
        最近更新 更多