I have this classes

   Key int ID
   string Street
   string City
   Country Country;
   Key int ID

I've got a edit form where Countries are shown as a combobox storing the ID and Name. Is it possible to assign the Country instance without loading the Country instance by ID / Key? This will save a lot of 'useless' loads from the database (a FK constraint will check at database level if the country still exists)..


This thread was imported from our support forum. The original discussion may contain more detailed answer.

asked Nov 20 '09 at 20:41

Marco's gravatar image


One Answer:

No, this is not possible: when you set a persistent reference property, its value is passed through a set of events. Earlier I thought if we must provide a workaround for this or not, and finally decided this is not necessary: 1) Global cache will completely eliminate the effect (database hit) you're avoiding. The only thing you need is to wait till its appearance. 2) Another way to eliminate the hit is to attach DisconnectedState to the Session - in fact, it is attachment of local cache. 3) We have a lot of stuff allowing to group database hits together (I mean future queries, prefetch API, .In / .Contains and so on). So even if there is a hit (and you really want it to happen - to deal with definitely actual data), it will be easy do make it running much faster.

Forgot to add: you can read reference field value without potential database hits, if this is necessary: see Persistent.GetReferenceKey (it is a protected method).

Although I'd recommend to use prefetch API instead of this. This method is added for pretty limited use cases (mainly, for our internal processes, such as reference cleanup on removal).

answered Nov 22 '09 at 11:30

Alex%20Yakunin's gravatar image

Alex Yakunin

Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!
toggle preview

powered by OSQA