2011-01-05 134 views
0

映射非所需的對象我有一個複合對象建立項目 - >評價,我評議對象有哪些不需要ApprovedMentor對象,但是當我去保存項目Nhib罰球和錯誤說ApprovedUser有未被設置。但沒有設置,因爲它不是必填字段。我如何使用流暢的自動映射來設置它,這有可能嗎?功能NHibernate使用自動映射

public class MentoringProject : BaseEntity 
{ 
    public MentoringProject() 
    { 
     Appraisal = new Appraisal(); 

    } 

     [NotNullNotEmpty] 
     [Length(Min=25, Max=1000)] 
     public virtual string Description { get; set; } 

     [Length(Min=25, Max=1000)] 
     public virtual string SupportRequired { get; set; } 

     [NotNullNotEmpty] 
     public virtual System.DateTime? DateSubmitted { get; set; } 

     [NotNullNotEmpty] 
     public virtual System.DateTime? ClosingDate { get; set; } 

     [NotNullNotEmpty] 
     [Size(Min=1)] 
     public virtual short Duration { get; set; } 

     [NotNullNotEmpty] 
     public virtual string Skills { get; set; } 


     public virtual Appraisal Appraisal { get; set; } 

}

public class Appraisal : BaseEntity 
{ 
    public Appraisal() 
    { 
     ShortlistedMentors = new List<User>(); 
     ApprovedMentor = new User(); 
     College = new RefData(); 
    } 

    #region Primitive Properties 

    public virtual bool Decision { get; set; } 

    public virtual System.DateTime? ApprovedDate { get; set; } 

    public virtual System.DateTime? AcceptedDate { get; set; } 

    public virtual System.DateTime? CompletionTargetDate { get; set; } 

    public virtual string RejectionReason { get; set; } 

    #endregion 

    #region Navigation Properties 

    public virtual IList<User> ShortlistedMentors { get; set; } 

    public virtual User ApprovedMentor { get; set; } 

    public virtual RefData College { get; set; } 

    #endregion 
} 
+0

是User類的實體? – Vadim 2011-01-05 01:48:54

+0

是的,它是。我將其連接到各種對象的域模型中,像MentoringProject.AddedUser,MentoringProject.UpdatedUser的ShortlistedMentors也是用戶對象的集合,我想我應該只是確保ApprovedUser設置爲空時,我打電話救? – Simon 2011-01-05 07:34:06

回答

0

在我看來,你只是想忽略,你需要在你的映射類這樣做ShortlistedMentors屬性:

map.IgnoreProperty(P = > p.ShortlistedMentors);

這個答案被張貼在此question

+0

,我想設置可選的屬性是ApprovedMentor,用戶將不設置這個最初當對象被保存,但應在保存初始之後的某一時刻被保存。 – Simon 2011-01-05 07:34:29

0

我想我已經解決了這個問題,當將UI綁定到MVC中的控制器時,MVC創建一個空的User對象,並且因爲該對象具有使用nhib validator和nhib設置的必需字段,並且nhib試圖創建一個新的用戶對象,我得到了這一輪以檢查是否有用戶realtionship添加,如果不是我設置Appraisal.ApprovedMentor==null