My Fundamental Dumbnosity -- Attach/Detach etc.. Context Problem

5 views
Skip to first unread message

gayes...@gmail.com

unread,
Sep 30, 2015, 3:27:48 PM9/30/15
to BrightstarDB Users
I was getting an "other context..." when updating a class that had list<> values -- ie -- UserProfile contains UserProfileImages...I no longer get the error by implementing  ((BrightstarEntityObject)myobject).Attach(ctx, true); // added this to get rid of the 
but it does not save the UserProfile with the added image? I appreciate the read transaction through update but that isn't so important to the application. So basically the reads aren't bundled with the writes.

I have wrapped the functions (business logic) associated with each "entity" in the associated partial class. 
I have an update which  calls CtxAdddUpdateSave() {
using (var ctx = PreLoad.getBrightStar)
            {
                ((BrightstarEntityObject)myobject).Attach(ctx, true); // added this to get rid of the out of context to go away...
                try
                {                
                    ctx.AddOrUpdate(myobject);
                    ctx.SaveChanges();
//((BrightstarEntityObject)myobject).Detach();
-- If I add a Detach() here it wipes out the content of myobject except for the key field? Is that what it's supposed to do?
                }
                catch (Exception ee)
                {
...
}

If I have nested saves where for example:
UserProfileImage upi = new UserProfileImage();
upi.name = "fred";
upi.updateinsert();

where UserProfileImage updateinsert() {
if (this.UserProfileImageOID == null) {
SetKey()...
  CtxAddUpdateSave(this);
UserProfile up = UserProfile.Fetch(upi.UserOID); -- this must be the other context? 
up.AddImage(upi);
up.updateinsert();

UserProfile updateinsert() {
this.CtxAddUpdateSave(); --- I used to get an error "other context..." 
}

No it works fine but doesn't attach the image to the UserProfile -- the catch on CtxAddUpdateSave is never hit...
where UserProfile ...
...
     ICollection<IUserProfileImage> lstUserProfileImage { get; set; }
...
public void AddImage(UserProfileImage upi) {
 if (!hasImage(upi)) then lst...add(upi); 
 this.updateinsert()..


Not understanding this -- Since I cleared and reattached the context to the current ctx using by the CtxAdddUpdateSave -- why doesn't it save the UserProfile with the lstImage item added? It is there before the ctx.AddOrUpdate(myobject); is applied?

I am using (using ctx...) everywhere ... This is a web application. Is a better option to create one ctx at the beginRequest time (once per request) and use a single ctx for read/write activity? I have fetches and creates that create the entity.

k...@networkedplanet.com

unread,
Oct 2, 2015, 5:52:11 AM10/2/15
to BrightstarDB Users
Hi,

It sounds like you have quite a tricky attach/detach scenario here and it is possible that you may be hitting a bug or that you are doing something wrong but I can't quite follow what's going on in your app. I think what you suggest at the end is a better way to go for a web app. Typically when I use B* in a web app (I usually use MVC or Nancy) I will create the context in the scope of the controller. If you aren't using MVC, then creating the context at beginRequest time should also work.

Please can I ask you to post any follow-up questions to http://brightstardb.codeplex.com/discussions - as we no longer use this forum due to admin issues.

Cheers

Kal
Reply all
Reply to author
Forward
0 new messages