tags:

views:

508

answers:

1

I have these tables:

create table Brand ( ID int identity(1,1), Name varchar(100) not null unique, primary key clustered (ID) )

create table Product ( ID int identity(1,1), Name varchar(100) not null, BrandID int not null references Brand(ID) )

I have the respective entities in c#, namely the Brand entity has an int ID and string Name, and Product entity has int ID, string Name, Brand Brand.

How can I define a mapping such that when I create a new Product model, set its name, and set a new Brand model on its name, when I call ISession.SaveOrUpdate it will first check, using the Brand Name, whether that record exists? If I set the ID on Brand to be the identity, it will try to insert the record even if the name already exists, because I am not explicitly checking the existence of the record. If I set the name as the identity, then it does not think it needs to be inserted the fist time around. I can explicitly check if the brand exists beforehand, but I was looking for a mapping that would do this automatically. Something that will allow me to specify that the identity of the Brand should be verified by its name, but that a 0 ID indicates that its new.

+1  A: 

This can't be done via mapping since it's not a mapping concern.

bingle