nhibernate - Link two entities based on unique value not ID -


I have the following database tables Primary Key ID and AnimalType.Type is a unique string.

  Animals - ID - Name - TypeID AnimalType - ID - Type [Hervivor, Carnivore]  

My classes are as follows.

  Public category Animals {Public Entry ID {Receive; Private set; } Public string name {get; Set; } Public AnimalType type {get; Set; }} Public Square AnimalType {Private Int ID {get; Set; } Public string type {get; Set; }}  

How do I get the following code to work in NHibernate so that it herbivore

<> horse = New animal () {name = "horse", type = new animal type () {"herbivor"}}; Repository.SaveOrUpdate (horse); Var rabbit = new Animal () {name = "rabbit", type = new animal type () {"herbivor"}}; Repository.SaveOrUpdate (rabbit);

Update

If I can get NHibernate to do this argument well:

I'll check some options.

1) If the data does not change frequently, you can type an annuity instead of an entity object?

2) Instruct an object of the herbivor animal type using animal type repository.fundbytype ("herbivor") and pass that object into its new animal.

3) The above logic to the animal registers Save and update (animal) method so that you have ...

  public class AnimalRepository {public zero SaveOrUpdate (animal animal) {var animalType = animal.Type; If (animalType.ID == 0) {animal.Type = animalTypeRepository.Find (animalType.Type); } // save or update the animal ...}}  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -