How to match it? HasOne x Links

I need to do the “One by One” comparison, and I have some doubts. I have classes:

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 () { }
}

A DocumentType object has only one DocumentConfiguration, but it does not inherit, it is only one after another and unique to separate properties.

What should be my display in this case? Should I use links or HasOne? Can anyone give an example?

When I load the DocumentType object, I would like to automatically load the Configuration property (in the Type document).

Thanks a lot guys!

Greetings

0
source share
2 answers

, -- , , , --. , PK , , FK DocumentConfiguration DocumentType. , , , . DocumentType HasOne.Inverse.AllDeleteOrphan... DocumentConfiguration "".

, .

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

Source: https://habr.com/ru/post/1749794/


All Articles