Saving Parent and one to one association in one go

Posts   
 
    
chand
User
Posts: 72
Joined: 05-Aug-2007
# Posted on: 03-May-2009 01:35:36   

I made up the following scenario to state the problem

  1. Address Entity.
  2. State Entity has a unique constraint field StateCode.

AddressEntity address = new AddressEntity(); address.Street = "some street name";

// If I fetch the id for the state entity and use the following line. Save on address works fine.

// address.StateId = fetched StateId

// I don't want to fetch the stateid as I know the StateCode. I want to do something along the following lines

StateEntity state = new StateEntity(); state.IsNew = false; state.StateCode = "CA";

address.State = state;

adapter.SaveEntity(address, true);

But now save fails. There is an insert satement that is being generated for the state. Is there any way to achieve what I am trying to do?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 03-May-2009 21:39:35   

But now save fails. There is an insert satement that is being generated for the state. Is there any way to achieve what I am trying to do?

No, as far as I know, as the relation between Address and State isn't based on StateCode but StateId.

David Elizondo | LLBLGen Support Team
rdhatch
User
Posts: 198
Joined: 03-Nov-2007
# Posted on: 04-May-2009 05:12:19   

Hi Chand,

David is correct. If StateCode (ie, "CA") is not your State's primary key - then you will need to either fetch the State entity from the DB to get the StateID, or you will need to know the StateID when updating your address entity (ie, myAddress.StateID = myStateID).

If you will only need USA states, then you can use the StateCode as the primary key and get rid of StateID. However, if you plan to support States/Provinces & other countries, then you will likely need to keep your current setup and fetch the State entity to obtain the StateID.

You can simply create a helper method to fetch your State entity. For instance:

myState.FetchByStateCode("USA", "CA")
mAddress.State = myState

Hope this helps!

Ryan

Also... Technically, this is not a 1:1 relationship, but a 1:8. 1:1 relationships share primary keys.

chand
User
Posts: 72
Joined: 05-Aug-2007
# Posted on: 04-May-2009 13:35:53   

Yes you are right it is Many to One.

I am trying to map Address DTO into AddressEntity while doing I am trying to avoid any dataaccess code in the mapper assemblies. By the I am writing a REST service.

I would like to express my intention of associating an existing m:1 association through the LLBLGen entitiy construction, without having to make a fetch call from the mapper assemblies.

I was investigating whether there is any way to signal this via the save method or unitofwork to do the automatic fetch of the StateEntity given a predicate.

Is having this kind of facility at the DAL level a bad idea?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-May-2009 14:40:21   

At any point of time you should fetch the PK of the State entity.

Otherwise you might need to consider modifying the State Entity, to use a real PK (State Code), rather than the StateId.