Sitefinity 动态内容重复键异常

Sitefinity Dynamic Content Duplicate Key Exception

我们公司的主站点已经使用 Sitefinity 一年多了,我们有一个自定义小部件,它使用通过模块构建器以编程方式创建的模块,以及一个正在开发中的新自定义小部件。对于这两个小部件,我们最近开始偶尔收到以下错误:

Violation of PRIMARY KEY constraint ‘pk_sf_dynamic_content’. Cannot insert duplicate key in object ‘dbo.sf_dynamic_content’. The duplicate key value is (581f9be9-f9b8-6ac8-bf63-ff000000686f). The statement has been terminated.

我们无法找到它发生的时间模式。 Sitefinity 是否正在做某事导致这种情况?我们已经确认它没有被调用两次。作为最后的手段,手动设置项目的 ID 不起作用。

下面是导致抛出异常的代码片段:

DynamicModuleManager dynamicModuleManager = DynamicModuleManager.GetManager(String.Empty);

Type downloadType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.Download");
DynamicContent item = dynamicModuleManager.CreateDataItem(downloadType);

var downloadID = Guid.NewGuid().ToString();
item.SetValue("DownloadID", downloadID);
item.SetValue("DateDownloaded", DateTime.Now);

item.SetString("UrlName", downloadID);
item.SetValue("Owner", SecurityManager.GetCurrentUserId());
item.SetValue("PublicationDate", DateTime.Now);
item.SetWorkflowStatus(dynamicModuleManager.Provider.ApplicationName, "Published");

// Attach User & Resource
var userType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.User");
var userItem = dynamicModuleManager.GetDataItems(userType).FirstOrDefault(d => d.GetValue<string>("UserID").ToString().Equals(user.UserID) && d.Status == ContentLifecycleStatus.Master);
if (userItem != null)
{
    item.CreateRelation(userItem, "User");
}

var resourceType = TypeResolutionService.ResolveType("Telerik.Sitefinity.DynamicTypes.Model.Resources.Resource");
var resourceItem = dynamicModuleManager.GetDataItems(resourceType).FirstOrDefault(d => d.GetValue<string>("ResourceID").ToString().Equals(resource.ResourceId) && d.Status == ContentLifecycleStatus.Master);
if (resourceItem != null)
{
    item.CreateRelation(resourceItem, "Resource");
}

// Save Download
dynamicModuleManager.Provider.SuppressSecurityChecks = true;
dynamicModuleManager.SaveChanges(); //<== exception here

这通常是由于多个项目具有相同的 urlname。这是 sitefinty 或标题字段的唯一标识。在创建项目之前先查看一下以确保唯一性。