我正在使用Hibernate和amp;的结合使用C#的.NET Persistence API。我有两个实体之间的关系:
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; }
}
但我仍然得到同样的错误。任何人都可以建议我解决这个问题吗?
答案 0 :(得分: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; }
}