В запись уже добавлена ​​ошибка, хотя IsKey имеет значение false

Я получаю Entry has already been added ошибка при попытке использовать тот же заголовок для SubModule, Это происходит, хотя IsKey = false но все равно не позволяет вводить дубликаты Upload Как название

Как я могу исправить эту проблему?

Вот мой web.config

<SiteModules>
    <Modules>
      <MainModule title="MyUpload">
        <SubModule title="Upload" page="/script/upload1" groups="group1" display="true" type="maker"></SubModule>
        <SubModule title="Upload" page="/script/upload2" groups="group2" display="true" type="checker"></SubModule>
        <SubModule title="SomeTitle1" page="/script/upload3" groups="group1" display="false"></SubModule>
      </MainModule>
    </Modules>
  </SiteModules>

Вот мой класс

namespace MyClasses
{
    public class SiteModules : ConfigurationSection
    {
        [ConfigurationProperty("Modules", IsDefaultCollection = false)]
        public Modules Modules
        {
            get
            {
                Modules modulesConfigElement = (Modules)base["Modules"];
                return modulesConfigElement;
            }
        }
    }

    public class Modules : ConfigurationElementCollection
    {
        public Modules()
        {
            AddElementName = "MainModule";
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new MainModule();
        }
        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((MainModule)element).Title;
        }
    }

    public class MainModule : ConfigurationElementCollection
    {
        public MainModule()
        {
            AddElementName = "SubModule";
        }
        [ConfigurationProperty("title", IsRequired = true, IsKey = false)]
        public string Title
        {
            get
            {
                return (string)this["title"];
            }
            set
            {
                this["title"] = value;
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new SubModule();
        }
        protected override Object GetElementKey(ConfigurationElement element)
        {
            return ((SubModule)element).Title;
        }
    }

    public class SubModule : ConfigurationElement
    {
        [ConfigurationProperty("title", IsRequired = true, IsKey = false)]
        public string Title
        {
            get
            {
                return (string)this["title"];
            }
            set
            {
                this["title"] = value;
            }
        }

        [ConfigurationProperty("page", IsRequired = true)]
        public string Page
        {
            get
            {
                return (string)this["page"];
            }
            set
            {
                this["page"] = value;
            }
        }

        [ConfigurationProperty("groups", IsRequired = true)]
        public string Groups
        {
            get
            {
                return (string)this["groups"];
            }
            set
            {
                this["groups"] = value;
            }
        }

        [ConfigurationProperty("display", IsRequired = true)]
        public string Display
        {
            get
            {
                return (string)this["display"];
            }
            set
            {
                this["display"] = value;
            }
        }

        [ConfigurationProperty("type", IsRequired = false)]
        public string Type
        {
            get
            {
                return (string)this["type"];
            }
            set
            {
                this["type"] = value;
            }
        }
    }
}

Код, который выдает ошибку:

SiteModules siteModules = (SiteModules)ConfigurationManager.GetSection("SiteModules");

0 ответов

Исключение не связано с атрибутом IsKey. Посмотрите на переопределенный метод GetelementKey.
В данном случае он возвращает Title.. который не является уникальным.

    protected override Object GetElementKey(ConfigurationElement element)
    {
        return ((MainModule)element).Title;
    }
Другие вопросы по тегам