With Fluent NHibernate
, I have an arbitrary ClassMap<T>
, I want to be able to find out what property (if any) was set as the primary key
.
Example:
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(p => p.StupidPrimaryKeyId).GeneratedBy.Identity().Column("StupidPrimaryKeyId");
}
}
...
//usage
MemberInfo primaryKeyMember = FindPrimaryKey(new PersonMap());
Can anybody tell me what the method body for FindPrimaryKey
would have to be in order to return StupidPrimaryKeyId
?
Edit: 1/10/12
I originally wanted this because I wanted to know whether or not a detached entity existed in the database, based solely on the primary key (thus my need for knowing the primary key member, not string). I set down this path because a lot of this code already existed in our code base. After rethinking the issue, I've instead realized that the mapping should already take care of that, so using NHibernate.Linq I know have this:
public virtual bool RecordExists(TRecord obj)
{
var exists = _session.Query<TRecord>().Where(r => r == obj).Any();
return exists == false;
}