无法使用Fluent Nhibernate编写多态关系

时间:2015-12-17 07:37:26

标签: c# asp.net-mvc nhibernate orm fluent-nhibernate

我是Nhibernate的新手,我不知道如何在我的应用程序中创建多态关系/映射。我尝试了不同的解决方案和教程,但不幸的是我仍然无法弄清楚如何才能使这项工作成功。以下是我的数据库结构,其中ProcessActionLog是主表(基类),ProcessActionEmail,ProcessActionInterviewFeedback和ProcessActionNotesInfo是子实体。

enter image description here

实体

ProcessActionLog.cs

    /// <summary>
    /// Process action type enum field
    /// </summary>
    public enum ProcessActionType
    {
        ProcessActionEmail,
        ProcessActionInterviewFeedback,
        ProcessActionNotesInfo
    }
    public class ProcessActionLog
    {
        /// <summary>
        /// Id (primary key)
        /// </summary>
        public virtual int Id { get; set; }
        /// <summary>
        /// User on which this particular process and its corresponding task is performed
        /// </summary>
        public virtual User MasterUser { get; set; }
        /// <summary>
        /// User by whom this particular action is performed
        /// </summary>
        public virtual User SystemUser { get; set; }
        /// <summary>
        /// Process of this particular action
        /// </summary>
        public virtual Process Process { get; set; }
        /// <summary>
        /// Task of this particular action
        /// </summary>
        public virtual Task Task { get; set; }
        /// <summary>
        /// Name of action being performed
        /// </summary>
        public virtual string LogName { get; set; }
        /// <summary>
        /// Created at time stamp of this action log
        /// </summary>
        public virtual DateTime CreatedAt { get; set; }
        /// <summary>
        /// Process Action Type 
        /// </summary>
        public virtual ProcessActionType ProcessActionType { get; set; }
        /// <summary>
        /// Details of process action Log
        /// </summary>
        public virtual ProcessActionLog Details { get; set; }
    }

ProcessActionInterviewFeedback.cs

public class ProcessActionInterviewFeedback 
    {
        /// <summary>
        /// primary key of interview feedback record
        /// </summary>
        public virtual int Id { get; set; }
        /// <summary>
        /// Interviewer name
        /// </summary>
        public virtual string InterviewerName { get; set; }
        /// <summary>
        /// location
        /// </summary>
        public virtual string Location { get; set; }
        /// <summary>
        /// Date of interview 
        /// </summary>
        public virtual DateTime Date { get; set; }
        /// <summary>
        /// Process Action Log
        /// </summary>
        public virtual ProcessActionLog ProcessActionLog { get; set; }
    }

ProcessNotesInfo.cs

public class ProcessActionNotesInfo
    {
        /// <summary>
        /// Id of process action notes
        /// </summary>
        public virtual int Id { get; set; }
        /// <summary>
        /// comment given in notes
        /// </summary>
        public virtual string Comment {get; set;}
        /// <summary>
        /// Process Action log
        /// </summary>
        public virtual ProcessActionLog ProcessActionLog { get; set; }
    }

映射

ProcessActionLogMap.cs

class ProcessActionLogMap : ClassMap<ProcessActionLog>
    {
        public ProcessActionLogMap()
        {
            Id(x => x.Id);
            Map(x => x.LogName).Length(100).Not.Nullable();
            Map(x => x.ProcessActionType).CustomType<Int32>().Not.Nullable();
            Map(x => x.CreatedAt).CustomType<DateTime>().Not.Nullable();
            References(x => x.MasterUser).Column("UserId");
            References(x => x.Process).Column("ProcessId");
            References(x => x.SystemUser).Column("CreatedBy");
            References(x => x.Task).Column("TaskId").Nullable();

            //ReferencesAny(x => x.Details)
            //            .EntityTypeColumn("ProcessActionType")
            //            .EntityIdentifierColumn("Id")
            //            .IdentityType<int>()
            //            .AddMetaValue<ProcessActionEmail>("ProcessActionEmail").EntityIdentifierColumn("ProcessActionLogId")
            //            .AddMetaValue<ProcessActionInterviewFeedback>("ProcessActionInterviewFeedback").EntityIdentifierColumn("ProcessActionLogId")
            //            .AddMetaValue<ProcessActionNotesInfo>("ProcessActionNotesInfo").EntityIdentifierColumn("ProcessActionLogId");  
        }
    }

ProcessActionEmailMap.cs

class ProcessActionEmailMap : ClassMap<ProcessActionEmail>
    {
        public ProcessActionEmailMap()
        {
            Id(x => x.Id);
            Map(x => x.CC).Length(100).Nullable();
            Map(x => x.EmailBodyContent).Length(4001).Not.Nullable();
            Map(x => x.EmailDateTime).CustomType<DateTime>().Not.Nullable();
            Map(x => x.EmailRandomText).Length(100).Nullable();
            Map(x => x.EmailSubject).Length(100).Not.Nullable();
            Map(x => x.Recipients).Length(100).Not.Nullable();
            Map(x => x.RecipientType).CustomType<Int32>().Not.Nullable();
            References(x => x.ProcessActionLog, "ProcessActionLogId");
        }
    }

ProcessActionInterviewFeedback.cs

class ProcessActionInterviewFeedbackMap : ClassMap<ProcessActionInterviewFeedback>
    {
        public ProcessActionInterviewFeedbackMap()
        {
            Id(x => x.Id);
            Map(x => x.Date).CustomType<DateTime>().Nullable();
            Map(x => x.InterviewerName).Length(50).Not.Nullable();
            Map(x => x.Location).Length(100).Nullable();
            References(x => x.ProcessActionLog, "ProcessActionLogId");
        }
    }

ProcessActionNotesInfoMap.cs

class ProcessActionNotesInfoMap : ClassMap<ProcessActionNotesInfo>
    {
        public ProcessActionNotesInfoMap()
        {
            Id(x => x.Id);
            Map(x => x.Comment).Length(255).Not.Nullable();
            References(x => x.ProcessActionLog, "ProcessActionLogId");
        }
    }

现在我要找的是创建这样的映射,它基于ProcessActionLog表中的“ProcessActionType”列,我应该能够从任何子表中插入和检索数据。我在子表中定义主键的原因是因为对于父表的一列,子表可以在子表中有多个记录。请帮助。

ProcessActionLogMap.cs文件的注释部分给出了错误。

请帮助我完成这种关系。谢谢。

0 个答案:

没有答案