How to map it? HasOne x References

后端 未结 2 1503
情话喂你
情话喂你 2021-01-28 16:01

I need to make a mapping One by One, and I have some doubts. I have this classes:

public class DocumentType {    
    public virtual int Id { get; set; }    
            


        
相关标签:
2条回答
  • 2021-01-28 16:26

    If the relationship is really one-to-one... then use HasOne :-)

    See http://nhibernate.info/doc/nhibernate-reference/mapping.html#mapping-declaration-onetoone. It has all you need to know.

    0 讨论(0)
  • 2021-01-28 16:39

    Even though you have a one-to-one in your domain, your relational model is probably one-to-many. I doubt you share same PK on both tables, more likely you have FK on DocumentConfiguration for DocumentType. In that case you would map it as such, because what you are mapping is your relational model. So on DocumentType it would be HasOne.Inverse.AllDeleteOrphan ... and on DocumentConfiguration it would be "References".

    Now you domain should expose it as you are describing it.

    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; }
    
    0 讨论(0)
提交回复
热议问题