From cdc4c8632540c6d9999dc0d335ac5bb055e50d0a Mon Sep 17 00:00:00 2001 From: "agrath@gmail.com" Date: Thu, 16 Jun 2011 11:45:01 -0200 Subject: [PATCH] Added ContainsKey check to Language Dictionary to fix a KeyAlreadyExists exception on app startup. Fixes work item 30195 --- umbraco/cms/businesslogic/Dictionary.cs | 31 ++++++++++++++----------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/umbraco/cms/businesslogic/Dictionary.cs b/umbraco/cms/businesslogic/Dictionary.cs index 9649be13ce..028dde1b58 100644 --- a/umbraco/cms/businesslogic/Dictionary.cs +++ b/umbraco/cms/businesslogic/Dictionary.cs @@ -48,13 +48,16 @@ namespace umbraco.cms.businesslogic dr.GetGuid("id"), dr.GetGuid("parent")); - DictionaryItems.Add(item.key, item); + if (!DictionaryItems.ContainsKey(item.key)) + { + DictionaryItems.Add(item.key, item); + } } } cacheIsEnsured = true; } - } + } } } @@ -79,7 +82,7 @@ namespace umbraco.cms.businesslogic /// public class DictionaryItem { - + private string _key; internal Guid UniqueId { get; private set; } @@ -105,7 +108,7 @@ namespace umbraco.cms.businesslogic ensureCache(); var item = DictionaryItems.Values.Cast() - .Where(x => x.key == key) + .Where(x => x.key == key) .SingleOrDefault(); if (item == null) @@ -154,7 +157,7 @@ namespace umbraco.cms.businesslogic this.id = item.id; this._key = item.key; this.ParentId = item.ParentId; - this.UniqueId = item.UniqueId; + this.UniqueId = item.UniqueId; } private DictionaryItem _parent; @@ -244,7 +247,7 @@ namespace umbraco.cms.businesslogic { SqlHelper.ExecuteNonQuery("Update cmsDictionary set [key] = @key WHERE pk = @Id", SqlHelper.CreateParameter("@key", value), SqlHelper.CreateParameter("@Id", id)); - + //remove the cached item since the key is different DictionaryItems.Remove(key); @@ -260,16 +263,16 @@ namespace umbraco.cms.businesslogic dr.GetGuid("id"), dr.GetGuid("parent")); - DictionaryItems.Add(item.key, item); + DictionaryItems.Add(item.key, item); } else { throw new DataException("Could not load updated created dictionary item with id " + id.ToString()); } - } - + } + //finally update this objects value - this._key = value; + this._key = value; } } else @@ -449,7 +452,7 @@ namespace umbraco.cms.businesslogic SqlHelper.CreateParameter("@parentId", parentId), SqlHelper.CreateParameter("@dictionaryKey", key)); - using (IRecordsReader dr = + using (IRecordsReader dr = SqlHelper.ExecuteReader("Select pk, id, [key], parent from cmsDictionary where id=@id", SqlHelper.CreateParameter("@id", newId))) { @@ -473,8 +476,8 @@ namespace umbraco.cms.businesslogic { throw new DataException("Could not load newly created dictionary item with id " + newId.ToString()); } - } - + } + } else { @@ -506,7 +509,7 @@ namespace umbraco.cms.businesslogic { if (Deleting != null) Deleting(this, e); - } + } #endregion } }