I’ve an entity with assigned string Id on NHibernate and I’ve a little problem when get an entity by Id.
Example…
Suppose that have a database record like this…
Id Description
-------------------
AAA MyDescription
now, if I use “Get” method using search id “aaa”…
MYENTITYTYPE entity = Session.Get<MYENTITYTYPE>("aaa")
return right entity but Id field (entity.Id) is “aaa”, while I wish it were equal to “AAA”.
In summary I would like that “Get” method return the id identical to the one stored in the database…with the same case.
Is possible? How can I do?
Interesting question. My guess is that it’s not possible, because the Id might exist before the DB call. Consider the following:
This illustrates another reason why primary keys with meaning are usually a bad idea, especially if their input is not controlled.
Now, if instead of
Getyou use a query, you will get the right-cased Id:The drawback is that you will always go to the DB, even if the entity is already loaded.
Now, if you defined your entity as {Id, Code, Description}, where
Idis a synthetic POID (I recommend Hilo or Guid) andCodeis the existing string Id, you will avoid potential bugs caused by usingGetinstead of a query with the code.