本文介绍了如何映射它? HasOne x参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个一个地做一个映射,我有一些疑惑。我有这个类:

pre code public class DocumentType {
public virtual int Id {get;组; }
/ *为documenttype * /
设置属性public virtual DocumentConfiguration Configuration {get;组; }
public DocumentType(){}
}

public class DocumentConfiguration {
public virtual int Id {get;组; }
/ *一些其他的配置属性* /
public virtual DocumentType Type {get;组; }
$ b $ public DocumentConfiguration(){}
}

DocumentType对象只有一个DocumentConfiguration,但它不是继承,它只是一个接一个而且是唯一的,用来分隔属性。



在这种情况下,我的映射应该如何?我应该使用References还是HasOne?有人可以举个例子吗?



当我加载一个DocumentType对象时,我想自动加载属性Configuration(在documentType中)。

非常感谢!



干杯

解决方案

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

现在你的域名应该在你描述它的时候公开。

$ $ $ $ $ $ $ $ $ $ $ $ $ $ {
$ b $ $ _internalDocumentConfigurations = new List< DocumentConfiguration>(1);
}

私有的IList< DocumentConfiguration> _internalDocumentConfigurations
public virtual DocumentType Type
{
get
{
return _internalDocumentConfigurations.FirstOrDefault();

/ ** / WARNING - 这里没有setter **
}
公共虚拟SetDocumentConfiguration(DocumentConfiguration配置)
{
//添加断言这里
Add(config);

$ b private virtual Add(DocumentConfiguration config)
{
//在这里添加断言
_internalDocumentConfigurations.Add(config)
config .DocumentType = this;

$ b public virtual Remove(DocumentConfiguration config)
{
_internalDocumentConfigurations.Remove(config)
config.DocumentType = null;

code


$ b $ pre> public class DocumentConfiguration {
public virtual int Id {get;组; }
/ *一些其他的配置属性* /
public virtual DocumentType Type {get;内部保护组}


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; }    
    /* 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 is not a inherits, it's only one by one and unique, to separate properties.

How should be my mappings in this case ? Should I use References or HasOne ? Someone could give an example ?

When I load a DocumentType object I'd like to auto load the property Configuration (in documentType).

Thanks a lot guys!

Cheers

解决方案

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

这篇关于如何映射它? HasOne x参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 23:42