views:

59

answers:

2

Hi,

I have two classes; Area and LanguageArea. I use LanguageArea to handle translated Areas.

Area.cs
string Title,
string Description,
Pictures(set of Picture class),
LanguageAreas(set of LanguageArea class)

LanguageArea.cs
Language(defines which language area is translated in),
Area,
Title (translated title for the Area),
Description (translated description for the area)

For example when a translated area requested from the UI i get the LanguageArea first

public Area GetByLanguageId(long areaId, byte LanguageId)
        {
            var langArea = db.LanguageArea
                            .Where(i => i.Area.id == areaId && i.Language.id ==  LanguageId).FirstOrDefault();
            Area newArea = null;    
            if (langArea != null && langArea.Area != null)
            {
              newArea = langArea.Area; // I have copied to area to get area with pictures in it
              newArea.Title = langArea.Title; // I got the translated Title
              newArea.Description = langArea.Description; // I got the translated description
            }    
            return newArea;
        }

With the method above i get the the whole area object with the translated Title and Description. But translated title and description saved into ObjectContext. After that when the default language or any other language requested from UI, it still got the translated title and description from ObjectContext, not the data comes from DB.

I have tried MergeOption.OverwriteChanges. This time everthing was ok but when saving it saved the translated info not the original one which is on the DB.

I have tried Context.Refresh(); but it looks like a temporary solution.

What do you recommend to handle this situation ?

Thanks

+1  A: 

When you get the language area object it is tracked by ObjectContext. So, the Area object that you are passing to newArea is tracked as well. As I understand you are using the Area object just in presentation purposes and you don't want changes to be saved.

If this is true, calling Detach method on ObjectContext before returning newArea object may solve the problem because ObjectContext will stop tracking Area object, and changes will not be persisted.

db.Detach(newArea); return newArea;
Misha N.
It sounds useful, i am going to try it now and get back here if it works or not
Barbaros Alp
Thank you so much
Barbaros Alp
When i detach the newArea from ObjectContext it normally also detaches the sub collections of Area(Pictures and LanguageAreas) but i need these sub collections. Do you have any advice for this ? Thanks
Barbaros Alp
No problem. For detached collections I would probably do the same as you have. But I was confused why does it detach collection in the first place. I thought that EF when calling Detach just destroys ObjectStateEntry for each object in graph, not the relationship objects.
Misha N.
A: 

I did it. After db.Detach(newArea); for the pictures i wrote down these lines

.
.
.
db.Detach(a);
var pics = db.Picture.Where(p => p.Area.id == a.id).AsEnumerable()
                     .Select(p => new Picture() 
           {IsProfile = p.IsProfile, id = p.id, Path = p.Path, Title = p.Title});
if (pics.Count()>0)
    foreach (var pic in pics)
       a.Pictures.Add(pic);

return a;

I hope this helps someone else

Thanks

Barbaros Alp