开发者

Nhibernate - Referencing transient objects in disconnected sessions

I've been banging my head against a brick wall for nearly two days on this so any help would be great.

The scenario I've got is basically this.

I have an object called Campaign which I'm creating from scratch in a web client. The Campaign has a reference to another object, Portal. The Portal has been pulled from the database through NHibernate.

When I come to call CreateCampaign(Campaign cmp) NH barfs saying that the reference to Portal is transient and must be saved first. If i create EVERYTHING from scratch then it works fine.

So in short I have a new Campaign instance which references a detached ins开发者_开发问答tance of Portal. The cascades are set to 'save-update, merge'.

Do I need to reload all of the detached instances back into the session before I can call session.Save or is it something else I'm not aware of.

Sorry if this seems a little vague and for the lack of any code,..NDAs stop me from posting and code.

Thanks in advance.


You can use ISession.Lock to attach your transient object to the same session that is being used to persist Campaign:

session.Lock(myPortal, LockMode.None);
myCampaign.Portal = myPortal;
session.Save(myCampaign);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜