I have the following database tables. Primary keys are ID and AnimalType.Type is a unique string.
Animal
- ID
- Name
- TypeID
AnimalType
- ID
- Type [Herbivore, Carnivore]
My classes are as follows.
public class Animal
{
public int ID { get; private set; }
public string Name { get; set; }
public AnimalType Type { get; set; }
}
public class AnimalType
{
private int ID { get; set; }
public string Type { get; set; }
}
How would I get the following code to work in NHibernate so that it references the same AnimalType of Herbivore?
var horse = new Animal() { Name = "Horse", Type = new AnimalType() { "Herbivore" }};
repository.SaveOrUpdate(horse);
var rabbit = new Animal() { Name = "Rabbit", Type = new AnimalType() { "Herbivore" } };
repository.SaveOrUpdate(rabbit);
UPDATE
Be nice if I could get NHibernate to perform this logic: http://andreas.scherbaum.la/blog/archives/11-Avoid-Unique-Key-violation.html