Skip to content

Commit e3d24b6

Browse files
committed
NodeCollection: Better error exception on adding items with same name
1 parent 8716945 commit e3d24b6

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Orm/Xtensive.Orm/Sql/Model/NodeCollection.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@ public class NodeCollection<TNode>: CollectionBaseSlim<TNode>
3030
/// <returns><see langword="True"/> if this instance is read-only; otherwise, <see langword="false"/>.</returns>
3131
public override bool IsReadOnly { get { return IsLocked || base.IsReadOnly; } }
3232

33-
/// <inheritdoc/>
33+
/// <summary>
34+
/// Adds item to collection.
35+
/// </summary>
36+
/// <param name="item">Item to add</param>
37+
/// <exception cref="ArgumentException">The item with same name already exists in the collection</exception>
3438
public override void Add(TNode item)
3539
{
3640
base.Add(item);
37-
if (!string.IsNullOrEmpty(item.GetNameInternal()))
38-
nameIndex.Add(item.GetNameInternal(), item);
41+
var name = item.GetNameInternal();
42+
if (!string.IsNullOrEmpty(name) && !nameIndex.TryAdd(name, item)) {
43+
throw new ArgumentException(string.Format(Strings.ExItemWithNameXAlreadyExists, name));
44+
}
3945
}
4046

4147
public override bool Remove(TNode item)

0 commit comments

Comments
 (0)