0

我正在使用 Hibernate & NET Persistence 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; }
}

但我仍然遇到同样的错误。谁能建议我解决这个问题?

4

1 回答 1

0

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

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; }
}
于 2012-11-01T11:44:58.357 回答