そのため、データベース アクセスに NHibernate を使用するのは初めてで、編集中のアプリケーションの他の場所でその使用法を調べた後、それが機能するようには見えず、理由がわかりません。事実上、オブジェクトにデータベースのデータを入力して、断片を取り出してユーザーに提示できるようにしようとしています。問題は、構文とコードが正しいように見えても、クエリの実行後にオブジェクトが null のままになることです。
データベース内のテーブルを表すために使用されているクラス:
public class AllocateLog
{
public virtual string UserName { get; set; }
public virtual int Id { get; set; }
public virtual int OwnerId { get; set; }
public virtual int MemberId { get; set; }
public virtual int? ResId { get; set; }
public virtual string RequestComments { get; set; }
public virtual DateTime DateEntered { get; set; }
public virtual DateTime? DateExited { get; set; }
public virtual string EntryAccessPoint { get; set; }
public virtual string ExitAccessPoint { get; set; }
}
マッピング コード:
public class AllocateLogOverride : IAutoMappingOverride<AllocateLog>
{
public void Override(AutoMapping<AllocateLog> map)
{
#if LOCAL_INSTALL
map.Schema("dbo");
#else
map.Schema("cred");
#endif
map.Table("allocate_log");
map.CompositeId()
.KeyProperty(x => x.OwnerId, "owner_id")
.KeyProperty(x => x.MemberId, "member_id")
.KeyProperty(x => x.DateEntered, "date_entered");
map.Map(x => x.UserName).Column("user_name");
map.Map(x => x.ResId).Column("res_id");
map.Map(x => x.RequestComments).Column("request_comments");
map.Map(x => x.DateExited).Column("date_exited");
map.Map(x => x.EntryAccessPoint).Column("entry_access_point");
map.Map(x => x.ExitAccessPoint).Column("exit_access_point");
}
}
クエリ コード:
public class AllocateLogsForAccessPoints : IQuery<IQueryOver<AllocateLog>, AllocateLog>
{
private readonly string accessPoint;
public AllocateLogsForAccessPoints(string accessPoint)
{
this.accessPoint = accessPoint;
}
public IQueryOver<AllocateLog> BuildQuery(ISession session)
{
return session.QueryOver<AllocateLog>()
.Where(d => d.EntryAccessPoint == accessPoint);
}
public AllocateLog Execute(IQueryOver<AllocateLog> query)
{
return query.SingleOrDefault();
}
}
そして、クエリがオブジェクトに何かを返すかどうかを確認するためのテストとして使用しているコード:
var asdf = DbQueryExecutor.ExecuteQuery(new AllocateLogsForAccessPoints((string)"north gate"));
entry_access_point にデータがあり、文字列が "north gate" であるデータベース内の唯一の行であるため、そのクエリに適合するデータベース内のレコードは 1 つだけです。テーブルは cred.allocate_log で、すべての列はマッピング ファイルで正しく名前が付けられています。さらに、マッピング ファイルを削除したり、コメント アウトしたりしても実行時エラーは発生しません。つまり、NHibernate がマッピング ファイルを使用しようとさえしないことを意味します。クエリが機能する他のマッピング ファイルを使用すると、実行時エラーが発生します。したがって、エラーなしで実行されるクエリを実行した後、オブジェクトが null のままである理由について、私は完全に困惑しています。何か案は?さらに情報が必要な場合は、元の投稿を更新します。