Thanks for your article.
However I have problem.
I would like to generate ID based on the certain field in the entity.
For example:
class Customer
{
private string _id;
private string _name;
}
I would like to generate id such as:
<name> + <next available id>
The <name> is the name of customer and the <next available id> get the next id automatically.
My question is: how do you pass the name of customer to id generator?
Regards,
Revin.
Sorry I have not been checking this post, I think I need to subscribe the comments.
I'm not sure why you would want to do this exactly, it seems a bit like using business values as a primary key (you should read the nhibernate documentation about why you should always use surrogate keys). Perhaps if you could tell me what your goal is with this strategy I might have a better solution for you.
Anyways, if you do need to do this (perhaps you are satisfying a custom database) and you want to use a custom Id generator, you could use reflection. You would add another property to your custom id generator like "prefix = "Name"" and then you could access that from your custom id generator and use it to get the value of the "name" property during the id generator process.
I don't recommend this really, primary key's should not really change on update and technically name is a property that could be changed.
This is a really intriguing idea for sure! However, after reading what Chris Nicola has said here, I think there is some point to what he has said. After all, primary keys are best left unchanged where the property name do get changed. I think the reflection method is better as it is quite easier to do and takes up pretty less time by adding another property to the id generator as when compared with this method!
www.magneticmessenger.com
hi, I'm having a lot of preblems using nhibernate. I can not get any of the sample code fromany of the tutorials at bytesland.com/nhibernate to work. Can you please tell me where I can download the source code you are using that way I can compare what I have done to what actually works and see what is wrong. I'm also not sure if I have instally or downlaod the correct version of Nhibernate.
www.bestpaintsprayers.us