简体   繁体   English

代号为“ XX”的设置键已存在

[英]The settings key with code name 'XX' already exists

I am trying to save a setting in Kentico and I get this error: 我试图在Kentico中保存设置,但出现此错误:

The settings key with code name 'AvalaraOrderStatus' already exists. 代号为“ AvalaraOrderStatus”的设置键已存在。

I already created the setting and I have saved a value to it. 我已经创建了设置,并且已经保存了一个值。 The code worked fine in Kentico 8, but I was asked for no SiteInfiIdentifer. 该代码在Kentico 8中运行良好,但是没有要求我提供SiteInfiIdentifer。

Here is the code I created to make the setting: 这是我创建以进行设置的代码:

//if the setting does not exist, then create it
if (SettingsKeyInfoProvider.GetSettingsKeyInfo(siteName + ".AvalaraOrderStatus", siteID) == null)
{
    // Create and set up new SettingsKey
    SettingsKeyInfo si = new SettingsKeyInfo();
    si.KeyName = "AvalaraOrderStatus";
    si.KeyDisplayName = "Avalara Order Status";
    si.KeyDescription = "Avalara order status for this site";
    si.KeyType = "string";
    si.KeyValue = string.Empty;
    si.KeyCategoryID = category.CategoryID;
    SettingsKeyInfoProvider.SetSettingsKeyInfo(si);
}

The code throws the error on the last line. 代码在最后一行抛出错误。 Here is my code: 这是我的代码:

int currentSiteID = CMS.SiteProvider.SiteContext.CurrentSiteID;
SiteInfoIdentifier siteId = new SiteInfoIdentifier(currentSiteID);

//update settings in system
SettingsKeyInfoProvider.SetValue(siteName + ".AvalaraOrderStatus", siteId, orderStatus.Trim());

A few things to note: 注意事项:

  • The first parameter of the SettingsKeyInfoProvider.GetSettingsKeyInfo method does not need to be prefixed with the site name. SettingsKeyInfoProvider.GetSettingsKeyInfo方法的第一个参数不需要以站点名称为前缀。 This is why a site identifier is provided (in your case, the SiteID ). 这就是提供站点标识符(在您的情况下为SiteID )的原因。 Otherwise, you might be getting a null value every time the if statement evaluates, which is why the setting key is being recreated even if it exists. 否则,每次if语句求值时,您可能会得到一个null值,这就是为什么即使存在该设置键也要重新创建的原因。 So that should be: 因此应该是:
    SettingsKeyInfoProvider.GetSettingsKeyInfo("AvalaraOrderStatus", siteID)
  • The same applies for the SettingsKeyInfoProvider.SetValue method - no need to prefix the site name: 这同样适用于SettingsKeyInfoProvider.SetValue方法-无需在站点名称前添加前缀:
    SettingsKeyInfoProvider.SetValue("AvalaraOrderStatus", siteId, orderStatus.Trim())
  • The CurrentSiteID integer is a valid SiteIdentifier, so there is no need to explicitly instantiate a SiteInfoIdentifier object: CurrentSiteID整数是有效的SiteIdentifier,因此无需显式实例化SiteInfoIdentifier对象:
    SettingsKeyInfoProvider.SetValue("AlavaraOrderStatus", CMS.SiteProvider.SiteContext.CurrentSiteID, orderStatus.Trim())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM