OneTomany关系在NHIBERNATE和净持久性API中不起作用



我正在使用Hibernate&净持久性API带有C#。我在两个实体之间有以下关系:

public class Question
{
    [OneToMany(Cascade = new CascadeType[] { CascadeType.ALL }, Fetch = FetchType.LAZY, MappedBy = "Question")]
    public virtual List<QueueLog> QueueLogs { get; set; }    
}

public class QueueLog
{
    [ManyToOne (Fetch = FetchType.EAGER, TargetEntity = typeof(Question))]
    [Column(Name = "question_id", Nullable = false)]
    public virtual List<Question> Question { get; set; }
}

但是,在通过其主要密钥ID提出问题时,我会遇到以下错误:

Missing column: Question_0 in Kmasters.dbo.queue_log
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: NHibernate.HibernateException: Missing column: Question_0 in Kmasters.dbo.queue_log

另外,我尝试通过以下方式建立两个实体之间的关系:

public class Question
{
    [OneToMany(Cascade = new CascadeType[] { CascadeType.ALL }, Fetch = FetchType.LAZY)]
    [JoinColumn(Name = "question_id", ReferencedColumnName = "id")]
    public virtual List<QueueLog> QueueLogs { get; set; }    
}

public class QueueLog
{
    [ManyToOne (Fetch = FetchType.EAGER, TargetEntity = typeof(Question))]
    [Column(Name = "question_id", Nullable = false)]
    public virtual List<Question> Question { get; set; }
}

,但我仍然遇到同样的错误。谁能为此建议我解决方案?

我通过进行以下更改已修复了代码:

public class Question
{
   [OneToMany(TargetEntity = typeof(QueueLog), MappedBy = "question", Fetch = FetchType.EAGER, Cascade = new CascadeType[] { CascadeType.ALL })]
   public virtual NHibernate.Collection.PersistentBag QueueLogs { get; set; }
}

public class QueueLog
{
     [ManyToOne (Fetch = FetchType.EAGER, TargetEntity = typeof(Question))]
     [JoinColumn(Name = "question_id")]
     public virtual Question question { get; set; }
}

最新更新