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;
}
So... I inspected
Fluent-Nhibernate
dll withReflector
and this is what I came-up with:Edit by viggity
This is what I was really looking for. Thanks again!
end edit
PKName (if the column name assigned explicitly) will obtain the "StupidPrimaryKeyId" column name.
I must say that I'm curious to know why do you need it.