Getting 'Identity can not be changed' error

Mar 29, 2012 at 8:33 PM

Hello, 

I saw there another question about this exception, but I did not found a solution.

Here is my load context:

public class IdLoadContext : LoadContext
    {
        public string Id
        {
            get { return (string) Identity; }
        }

        public IdLoadContext(string id) : base(id) {}
    }

Here is my view model:

    [CachePolicy(CachePolicy.CacheThenRefresh, 3600 * 7)]
    public class ShotViewModel : ModelItemBase<IdLoadContext>
    {
        public ShotViewModel() {}

        public ShotViewModel(string id) : base(new IdLoadContext(id)) {}

        public string Id
        {
            get { return (string) LoadContext.Identity; }
        }

        #region Properties

        private string _title;
        [JsonProperty("title")]
        public string Title
        {
            get { return _title; }
            set
            {
                if (_title != value)
                {
                    _title = value;
                    RaisePropertyChanged("Title");
                }
            }
        }

        private Uri _url;
        [JsonProperty("url")]
        public Uri Url
        {
            get { return _url; }
            set
            {
                if (_url != value)
                {
                    _url = value;
                    RaisePropertyChanged("Url");
                }
            }
        }



        private Uri _shortUrl;
        [JsonProperty("short_url")]
        public Uri ShortUrl
        {
            get { return _shortUrl; }
            set
            {
                if (_shortUrl != value)
                {
                    _shortUrl = value;
                    RaisePropertyChanged("ShortUrl");
                }
            }
        }


        private Uri _imageUrl;
        [JsonProperty("image_url")]
        public Uri ImageUrl
        {
            get { return _imageUrl; }
            set
            {
                if (_imageUrl != value)
                {
                    _imageUrl = value;
                    RaisePropertyChanged("ImageUrl");
                }
            }
        }
        
        private DateTime _createdAt;
        [JsonProperty("created_at")]
        public DateTime CreatedAt
        {
            get { return _createdAt; }
            set
            {
                if (_createdAt != value)
                {
                    _createdAt = value;
                    RaisePropertyChanged("CreatedAt");
                }
            }
        }
        
        #endregion

        public class ShotViewModelDataLoader : IDataLoader<IdLoadContext>
        {
            private const string GetShotMethod = "shots/{0}";

            public LoadRequest GetLoadRequest(IdLoadContext loadContext, Type objectType)
            {
                string uri = String.Format(Settings.Instance.Host + GetShotMethod, loadContext.Id);
                return new WebLoadRequest(loadContext, new Uri(uri));
            }

            public object Deserialize(IdLoadContext loadContext, Type objectType, Stream stream)
            {
                ShotViewModel shot = new ShotViewModel(loadContext.Id);
                string data;

                using (StreamReader reader = new StreamReader(stream))
                {
                    data = reader.ReadToEnd();
                }

                shot = JsonConvert.DeserializeObject<ShotViewModel>(data);
                return shot;
            }
        }
    }

Every time I try to bind it to my view I get 'Identity can not be changed' exception. I cannot find out what is wring with my code. 

I'd appreciate any help.

Coordinator
Mar 29, 2012 at 10:28 PM

Hi -

I think the problem is because you are over writing the value "shot" in deserialize.  The DeserializeObject call creates a new object which probably doesn't have a load context.

I suspect making this change will fix it:

shot = JsonConvert.DeserializeObject<ShotViewModel>(data);
show.LoadContext = loadContext;
return shot;

 

Mar 30, 2012 at 11:28 AM

Works great now!

Thanks, Shawn.