Fluent NHibernate使用自动映射映射非必需对象

时间:2011-01-05 01:29:14

标签: fluent-nhibernate automapping

我有一个复合对象设置Project-> Appraisal,我的评估对象有一个ApprovedMentor对象,这是不需要的,但是当我去保存项目Nhib抛出并且错误时说没有设置ApprovedUser。但它没有设置,因为它不是必填字段。如何使用流畅的自动映射设置它,是否可能?

 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
}

2 个答案:

答案 0 :(得分:0)

在我看来,您只是想忽略您需要在映射类中执行的ShortlistedMentors属性,如下所示:

map.IgnoreProperty(p =&gt; p.ShortlistedMentors);

此答案已发布在此question

答案 1 :(得分:0)

我想我已经解决了这个问题,当在MVC中将UI绑定到控制器时,MVC会创建一个空的User对象,并且因为该对象使用nhib验证器在其上设置了必需的字段,并且nhib试图创建一个新的用户对象,我通过检查是否有要添加的用户权限来解决这个问题,如果没有我设置Appraisal.ApprovedMentor==null