views:

22

answers:

2

i have the following entities:

public class Worker
{
 public int WorkerID {get;set;}
 public string Name { get;set;}
 public int version { get;set;}
}

public class TransferOrder
{
  public int TransferOrderID { get;set;}
  public Worker workerTobeTransfered{get;set;}
  public int version { get;set;}    
}

and i am using the Auto mapping, in fluent nhibernate. when i try to save the TransferOrder like this:

TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1};
Session.Save(order);

but in database, the workerID in the TransferOrder table is NULL??? but when i give a version to the worker, it is saved as normal?

TransferOrder order = new TransferOrder();
order.Worker = new Worker(){WorkerID = 1,Version = 1};
Session.Save(order);

notice that it is not important what version number is given to the worker as long as it is not 0. and i have a worker saved in the database with workerID = 1.

how can i handle this ? why should i give a version to the worker???is the nhibernate making sure that worker is saved?? and why it should do that ?

A: 

Maybe this is the scenerio:

Your Work.WorkerID is an identity column within your Work table and, you cannot insert a row into your table that consists of only an identity column entry.

But when you provide a value for Work.Version, as well, then you are creating a valid insert.

Nicholas Murray
i'm afraid that is not the situation, in the transferOrder table, the WorkerID is foreign key for the Worker table.
Nour Sabouny
@Nour - possibly the same applies to foreign keys, eg an insert will not be valid if the only value being inserted is an insert into a field defined as a Key?
Nicholas Murray
well, how can i make sure? is there any documentation about this situation?
Nour Sabouny
A: 

The "version" is probably going to be auto mapped to a NHibernate attribute for optimistic concurrency checking. I would use Fluent NHibernate to generate the mapping XML to see what it's using, as I can't find anything via Google about the default settings for auto mapped .

Rich