代码之家  ›  专栏  ›  技术社区  ›  Felipe Oriani

如何绘制地图?有一个x参考

  •  0
  • Felipe Oriani  · 技术社区  · 16 年前

    public class DocumentType {    
        public virtual int Id { get; set; }    
        /* othes properties for documenttype */  
        public virtual DocumentConfiguration Configuration { get; set; }
        public DocumentType () { } 
    }
    
    public class DocumentConfiguration {
       public virtual int Id { get; set; }
       /* some other properties for configuration */   
       public virtual DocumentType Type { get; set; }
    
      public DocumentConfiguration () { }
    }
    

    一个DocumentType对象只有一个DocumentConfiguration,但它不是一个inherits,它只是一个接一个且唯一的,用于分隔属性。

    加载DocumentType对象时,我希望自动加载属性配置(在DocumentType中)。

    谢谢大家!

    干杯

    2 回复  |  直到 16 年前
        1
  •  0
  •   epitka    16 年前

    即使您的域中有一对一的关系模型,您的关系模型也可能是一对多的。我怀疑您在两个表上共享相同的PK,更有可能在DocumentType的DocumentConfiguration上有FK。在这种情况下,您应该这样映射它,因为您映射的是您的关系模型。所以在DocumentType上它应该是HasOne.Inverse.AllDeleteOrphan。。。在文档配置中,它将是“引用”。

    现在您应该在描述它时公开它。

    public class DocumentConfiguration 
    {
        public DocumentConfiguration() 
        {
            _internalDocumentConfigurations = new List<DocumentConfiguration>(1);
        }  
    
       private IList<DocumentConfiguration> _internalDocumentConfigurations
       public virtual DocumentType Type 
       { 
         get 
         { 
            return _internalDocumentConfigurations.FirstOrDefault();
         } 
         /**/WARNING - no setter here**
       }
       public virtual SetDocumentConfiguration(DocumentConfiguration config)
       {
          //add your asserts here
          Add(config);
       }
    
       private virtual Add (DocumentConfiguration config)
       {
         //add your asserts here
          _internalDocumentConfigurations.Add(config)
           config.DocumentType = this;
       }
    
      public virtual Remove (DocumentConfiguration config)
      {
          _internalDocumentConfigurations.Remove(config)
          config.DocumentType = null;
      }
    

    public class DocumentConfiguration {
       public virtual int Id { get; set; }
       /* some other properties for configuration */   
       public virtual DocumentType Type { get;  protected internal set; }
    
        2
  •  0
  •   josliber Martin Ballet    10 年前

    如果关系真的是一对一。。。然后使用HasOne:-)

    看到了吗 http://nhibernate.info/doc/nhibernate-reference/mapping.html#mapping-declaration-onetoone . 它有你需要知道的一切。